ArgumentError in StaticPages#home - ruby-on-rails

I noob in Rails. I use rails 4.2.4. I have a problem with my contact form. I have an error: First argument in form cannot contain nil or be empty on the 3 line of this code (contact_form.html.erb):
<div class="all-banners-width">
<figure class="green-background">
<%= form_for #contact_form do |f| %>
<p>
<%= f.label :name %><br>
<%= f.text_field :name %>
</p>
<p>
<%= f.label :phone %><br>
<%= f.text_field :phone %>
</p>
<p>
<%= f.label :email %><br>
<%= f.text_field :email %>
</p>
<p>
<%= f.label :text %><br>
<%= f.text_field :text %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
</figure>
</div>
My contact_form_controller.rb is:
class ContactFormController < ApplicationController
def contact_form
#contact_form = ContactForm.new
end
def create
#contact_form = ContactForm.new(contact_form_params)
#contact_form.save
redirect_to root_path, notice: "Saved"
end
private
def contact_form_params
params.require(:contact_form).permit(:name, :phone, :email, :text)
end
end
My DB migration file is:
class CreateContactForms < ActiveRecord::Migration
def change
create_table :contact_forms do |t|
t.string :name
t.string :phone
t.string :email
t.string :text
t.timestamps null: false
end
end
end
If I change 3rd line from contact_form.html.erb to the next:
<%= form_for :contact_form do |f| %>
then I have not an error, but anyway contact form does not work and there are not any errors I can see.
Can somebody help me? I have not idea, where is problem.

You miss action new in controller:
def new
#contact_form = ContactForm.new
end
your template is rendered with #contact_form = nil.

Related

Reciving a 'NoMethodError in ContactsController#update' for some odd reason

I'm currently building a simple CRM and right now I'm trying to nest a feature which will allow users to assign tasks inside a contacts section.
So the issue...
When I click on the edit contact section than from the edit form when I click on the submit form I get a "NoMethodError in ContactsController#Update". But I never changed anything in my Contacts Controller.
Below is a screenshot of the whole error page.
The Error Page
Below is my entire contacts_controller.rb file.
class ContactsController < ApplicationController
def index
#contacts = Contact.all
end
def show
#contact = Contact.find(params[:id])
end
def new
#contact = Contact.new
end
def edit
#contact = Contact.find(params[:id])
end
def create
#contact = Contact.new(contact_params)
if #contact.save
redirect_to #contact
else
render 'new'
end
end
def update
#contact = Contact.find(params[:id])
if #contact.update(contact_params)
redirect_to #contact
else
render 'edit'
end
end
def destroy
#contact = Contact.find(params[:id])
#contact.destroy
redirect_to contacts_path
end
private
def contact_params
params.require(:contact).permit(:first_name, :last_name, :job_title, :twitter_url, :linkedin_url, :facebook_url, :medium_url, :email_field, :telephone_field)
end
end
I have no idea at all what happened to make my controller break...
If you need any of my other code please ask.
Thank you ahead of time!
=============================
This is my edit.html.erb view code:
<hr>
<h1>Edit Contact</h1>
<%= render 'form' %>
<%= link_to 'Back', contacts_path %>
This is my _form.html.erb code:
<%= form_with model: #contact, local: true do |form| %>
<p>
<%= form.text_field :first_name, placeholder: "Enter Contact Name" %>
</p>
<p>
<%= form.label :job_title %><br>
<%= form.text_field :job_title %>
</p>
<p>
<%= form.label :twitter_url %><br>
<%= form.text_field :twitter_url %>
</p>
<p>
<%= form.label :linkedin_url %><br>
<%= form.text_field :linkedin_url %>
</p>
<p>
<%= form.label :facebook_url %><br>
<%= form.text_field :facebook_url %>
</p>
<p>
<%= form.label :medium_url %><br>
<%= form.text_field :medium_url %>
</p>
<p>
<%= form.label :email_field %><br>
<%= form.text_field :email_field %>
</p>
<p>
<%= form.label :telephone_field %><br>
<%= form.text_field :telephone_field %>
</p>
<p>
<%= form.submit %>
</p>
<% end %>
This is my records in my model:
t.string :first_name
t.string :last_name
t.string :job_title
t.string :twitter_url
t.string :linkedin_url
t.string :facebook_url
t.string :medium_url
t.string :email_field
t.string :telephone_field
Here is my contact.rb model code
class Contact < ApplicationRecord
has_many :notes
has_many :tasks
validates :title, presence: true,
length: { minimum: 5}
end
You need to replace validates :title, presence: true,length: { minimum: 5} to validates :job_title, presence: true,length: { minimum: 5}

Rails: param is missing or the value is empty: stock

I keep getting an error when i try to create a new item for a database that displays ActionController::ParameterMissing in StocksController#create
param is missing or the value is empty: stock.
It says the source is at
private
def stock_params
params.require(:stock).permit(:product_name, :brand_name, :item_id, :upc_code, :color, :department, :size, :condition, :fabric_type, :shipping_weight, :sku, :asin, :quantity, :cost_price, :sell_price, :key_product_features, :product_description, :search_terms, :status, :listing_in_usa, :listing_in_canada, :listing_in_mexico)
end
Under my stockscontroller file on the terminal it says:
Started POST "/stocks" for 107.15.253.59 at 2017-05-22 16:38:14 +0000
Cannot render console from 107.15.253.59! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by StocksController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"poJrp+PWBi7bxgeauJ37VNh/Shyk0q6iCNA+5Y4UBIZ/rf5dXYVBOgtPpVPUbfzjLsn9Kcz/Fn6kHKXHG0dUuQ==", "stocks"=>{"product_name"=>"", "brand_name"=>"", "item_id"=>"", "upc_code"=>"", "color"=>"", "department"=>"", "size"=>"", "condition"=>"", "fabric_type"=>"", "shipping_weight"=>"", "sku"=>"", "asin"=>"", "quantity"=>"", "cost_price"=>"", "sell_price"=>"", "key_product_feature"=>"", "product_description"=>"", "search_terms"=>"", "status"=>"", "listing_in_usa"=>"", "listing_in_canada"=>"", "listing_in_mexico"=>""}, "commit"=>"Save Stocks"}
Completed 400 Bad Request in 1ms (ActiveRecord: 0.0ms)
ActionController::ParameterMissing (param is missing or the value is empty: stock):
app/controllers/stocks_controller.rb:48:in `stock_params'
app/controllers/stocks_controller.rb:15:in `create'
I basically repeated a code that is similar to this that works that i already created, but i can't find why this is wrong. I have 2 types of databases, Vendors and Stocks, but the Stocks database isn't working.
This is the code for stockscontroller/concerns/controllers/apps
class StocksController < ApplicationController
def show
#stock = Stock.find(params[:id])
end
def index
#stocks = Stock.all
end
def new
#stock = Stock.new
end
def create
#stock = Stock.new(stock_params)
if #stock.save
redirect_to #stock
else
render 'new'
end
end
def edit
#stock = Stock.find(params[:id])
end
def update
#stock = Stock.find(params[:id])
if #stock.update(stock_params)
redirect_to #stock
else
render 'edit'
end
end
def destroy
#stock = Stock.find(params[:id])
#stock.destroy
redirect_to stock_path
end
end
private
def stock_params
params.require(:stock).permit(:product_name, :brand_name, :item_id, :upc_code, :color, :department, :size, :condition, :fabric_type, :shipping_weight, :sku, :asin, :quantity, :cost_price, :sell_price, :key_product_features, :product_description, :search_terms, :status, :listing_in_usa, :listing_in_canada, :listing_in_mexico)
end
code for new/stocks/views/apps
<body>
<div class = "head5">
<h1>New Inventory</h1>
<div class = "header5">
<h3> Apparel Inventory</h3>
</div>
</div>
</body>
<%= form_for :stocks, url: stocks_path do |f| %>
<p>
<%= f.label :product_name %><br>
<%= f.text_field :product_name %>
</p>
<p>
<%= f.label :brand_name %><br>
<%= f.text_field :brand_name %>
</p>
<p>
<%= f.label :item_id %><br>
<%= f.text_field :item_id %>
</p>
<p>
<%= f.label :upc_code %><br>
<%= f.text_field :upc_code %>
</p>
<p>
<%= f.label :color %><br>
<%= f.text_field :color %>
</p>
<p>
<%= f.label :department %><br>
<%= f.text_field :department %>
</p>
<p>
<%= f.label :size %><br>
<%= f.text_field :size %>
</p>
<p>
<%= f.label :condition %><br>
<%= f.text_field :condition %>
</p>
<p>
<%= f.label :fabric_type %><br>
<%= f.text_field :fabric_type %>
</p>
<p>
<%= f.label :shipping_weight %><br>
<%= f.text_field :shipping_weight %>
</p>
<p>
<%= f.label :sku %><br>
<%= f.text_field :sku %>
</p>
<p>
<%= f.label :asin %><br>
<%= f.text_field :asin %>
</p>
<p>
<%= f.label :quantity %><br>
<%= f.text_field :quantity %>
</p>
<p>
<%= f.label :cost_price %><br>
<%= f.text_field :cost_price %>
</p>
<p>
<%= f.label :sell_price %><br>
<%= f.text_field :sell_price %>
</p>
<p>
<%= f.label :key_product_feature %><br>
<%= f.text_field :key_product_feature %>
</p>
<p>
<%= f.label :product_description %><br>
<%= f.text_field :product_description %>
</p>
<p>
<%= f.label :search_terms %><br>
<%= f.text_field :search_terms %>
</p>
<p>
<%= f.label :status %><br>
<%= f.text_field :status %>
</p>
<p>
<%= f.label :listing_in_usa %><br>
<%= f.text_field :listing_in_usa %>
</p>
<p>
<%= f.label :listing_in_canada %><br>
<%= f.text_field :listing_in_canada %>
</p>
<p>
<%= f.label :listing_in_mexico %><br>
<%= f.text_field :listing_in_mexico %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
<%= link_to 'Back', stocks_path %>
Code for the model
class Stock < ApplicationRecord
end
my routes/locals/config/app file
Rails.application.routes.draw do
get 'welcome/index'
resources :vendors
resources :stocks
root 'welcome#index'
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
my schema/migrate/db/config/apps
ActiveRecord::Schema.define(version: 20170522154349) do
create_table "stocks", force: :cascade do |t|
t.string "product_name"
t.string "brand_name"
t.integer "item_id"
t.integer "upc_code"
t.string "color"
t.string "size"
t.string "department"
t.string "condition"
t.string "fabric_type"
t.string "shipping_weight"
t.string "sku"
t.string "asin"
t.integer "quantity"
t.string "cost_price"
t.string "sell_price"
t.string "key_product_feature"
t.text "product_description"
t.text "search_terms"
t.string "status"
t.string "listing_in_usa"
t.string "listing_in_canada"
t.string "listing_in_mexico"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "vendors", force: :cascade do |t|
t.string "company"
t.string "contact_name"
t.string "phone"
t.string "email"
t.string "moq"
t.string "cost_per_item"
t.string "payment_method"
t.string "terms"
t.string "turnover"
t.string "returns"
t.text "notes"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
end
The new error says:
ActiveRecord::RecordNotFound in StocksController#show
Couldn't find Stock with 'id'=5
At the source on app/controllers/stocks_controller.rb:
class StocksController < ApplicationController
def show
#stock = Stock.find(params[:id])
end
def index
With the terminal saying:
ActiveRecord::RecordNotFound (Couldn't find Stock with 'id'=5):
app/controllers/stocks_controller.rb:3:in `show'
You're not returning a :stock key in your params.
Change
<%= form_for :stocks, url: stocks_path do |f| %>
Into
<%= form_for :stock, url: stocks_path do |f| %>
This seems to be a plural vs. sigular issue.
Your code is requiring a stock param, but it's getting stocks.
Try changing this line:
<%= form_for :stocks, url: stocks_path do |f| %>
to
<%= form_for :stock, url: stocks_path do |f| %>

undefined method ` ' for #<ContactsController:0x007fc3a821e300>

I have been following the tutorial (http://guides.rubyonrails.org/getting_started.html ) to create my own web app for create a contact manager.
When I want to create a new contact, I have code in my contacts_controller
class ContactsController < ApplicationController
def show
#contact = Contact.find(params[:id])
end
def new
end
def create
#contact = Contact.new(contact_params)
#contact.save
redirect_to #contact
end
private
def contact_params
params.require(:contact).permit(:firstname, :lastname, :email, :phonenumber, :notes)
end
end
In the view I have
<h1>New Contact</h1>
<%= form_for :contact, url: contacts_path do |f| %>
<p>
<%= f.label :firstname %><br>
<%= f.text_field :firstname %>
</p>
<p>
<%= f.label :lastname %><br>
<%= f.text_field :lastname %>
</p>
<p>
<%= f.label :email %><br>
<%= f.text_field :email %>
</p>
<p>
<%= f.label :phonenumber %><br>
<%= f.text_field :phonenumber %>
</p>
<p>
<%= f.label :notes %><br>
<%= f.text_area :notes %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
In routes.rb
Rails.application.routes.draw do
get 'welcome/index'
root 'welcome#index'
resources :contacts
end
However, when I submit the form, I got this error message.
I think I followed the tutorial correctly and have the new method in my class. This seems weird to me and I have no idea to start debugging this app.

Rails grab input value on submit in controller

I have a customer model that belongs to user. However, when creating the new customer, the user will not be signed in. Instead I will be assigning the user based on his name attribute.
Here is the customer migration:
create_table :customers do |t|
t.belongs_to :user
t.string :name, null: false
t.date :install_date, null: false
t.decimal :mmr, null: false
t.boolean :sixty_month, default: false
t.boolean :eft, default: false
t.boolean :activation, default: false
t.timestamps
end
I then have a new customer form:
<h1>Create Customer</h1>
<%= form_for #customer do |f| %>
<div>
<%= f.label :name %>
<%= f.text_field :name %>
</div>
<div>
<%= f.label :install_date %>
<%= f.date_field :install_date %>
</div>
<div>
<%= f.label :mmr %>
<%= f.number_field :mmr %>
</div>
<div>
<%= f.label :sixty_month %>
<%= f.check_box :sixty_month %>
</div>
<div>
<%= f.label :eft %>
<%= f.check_box :eft %>
</div>
<div>
<%= f.label :activation %>
<%= f.check_box :activation %>
</div>
<div>
<%= f.label :users_name %>
<%= f.text_field :users_name %>
</div>
<%= f.submit 'Create Customer' %>
<% end %>
I need to get the users_name field and match it with the associated user and take that users id and assign it to the customers :user_id attribute. In the customers controllers create action so far I have:
#customer.user_id = User.where("name = #{}").id
I need to pull to input from the users_name field in the new customer form and put it into the above code somehow. I just don't know how haha. All help is appreciated!
You will probably want to move this out of the controller and into a helper method but you could do like this:
def create
#customer = Customer.new(customer_params)
if params[:users_name]
user = User.find_by(name: params[:users_name])
#customer.user_id = user.id unless user.nil?
end
if #customer.save
flash[:success] = "Customer created!"
redirect_to #customer
else
render 'new'
end
end
This is assuming you are using Rails 4.

undefined method `merge' for nil:NilClass in a form using text_field_tag

I'm following this tutorial to create tags for a model (in my case the model Post):
controllers/posts_controller.rb:
def create
#user = current_user
#post = #user.posts.new(params[:post])
if #post.save
redirect_to #post, notice: 'post was successfully created.'
else
render action: "new"
end
#post.tag!(params[:tags])
end
views/posts/_form.html.erb:
<%= form_for(#post) do |f| %>
<%= render 'shared/error_messages' %>
<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :content %><br />
<%= f.text_area :content %>
</div>
<div class="field">
<%= f.label :tags %>
<%= f.text_field :tags, params[:tags] %>
</div>
<div class="actions">
<%= f.submit %>
</div>
views/posts/show.hmtl.erb:
<div class="tags">
<h4>Tags:</h4>
<%= render #post.tags %>
</div>
models/post.rb:
class Post < ActiveRecord::Base
has_and_belongs_to_many :tags
def tag!(tags)
tags = tags.split(" ").map do |tag|
Tag.find_or_create_by_name(tag)
end
self.tags << tags
end
end
models/tag.rb:
class Tag < ActiveRecord::Base
has_and_belongs_to_many :posts
end
db/migrate/(etc...)_create_tags.rb:
class CreateTags < ActiveRecord::Migration
def change
create_table :tags do |t|
t.string :name
end
create_table :tags_posts, :id => false do | t |
t.integer :tag_id, :post_id
end
end
end
Now, when I visit the posts form I get this error:
undefined method `merge' for nil:NilClass
Extracted source (around line #13):
10: </div>
11: <div class="field">
12: <%= f.label :tags %>
13: <%= f.text_field :tags, params[:tags] %>
14: </div>
15: <div class="actions">
16: <%= f.submit %>
When I visit a post I get this error:
SQLite3::SQLException: no such table: posts_tags: SELECT "tags".* FROM "tags" INNER JOIN "posts_tags" ON "tags"."id" = "posts_tags"."tag_id" WHERE "posts_tags"."post_id" = 7
Extracted source (around line #24):
21:
22: <div class="tags">
23: <h4>Tags:</h4>
24: <%= render #post.tags %>
25: </div>
26:
27: </div>
But I do have these tables as you can see in my schema.rb file:
create_table "tags", :force => true do |t|
t.string "name"
end
create_table "tags_posts", :id => false, :force => true do |t|
t.integer "tag_id"
t.integer "post_id"
end
Any suggestions to fix this?
Just in case anyone else comes across this old question through google:
You need to use 'value:' for the default value in a simple form.
<%= f.text_field :tags, value: params[:tags] %>
You have the table name backwards. HABTM looks for the models alphabetically. Look at the error carefully. It says posts_tags cannot be found. You create tags_posts. So change your table name to posts_tags.
try this <%= f.text_field_tag :tags, params[:tags] %> in your partial _form.html.erb.
<%= form_with(
url: order_register_path(session[:order_id]),
method: :put
) do |form| %>
<%= form.check_box :review_budget %>
<%= form.check_box :fast_delivery %>
<%= form.check_box :replace_similar %>
<div>
<%= f.submit "Continuar" %>
</div>
<% end %>
f.submit instead of that submit_tag

Resources