james
james

Reputation: 4049

Params is nil even though log shows passing

Paramaters from Rails debugger shows

{"utf8"=>"✓",
  "authenticity_token"=>"m2NMruoFRr6lpsuVMK9UthlY0bsJsPmf1LWce2uKaH4=",
  "signup_id"=>"5",
  ":item_name"=>["Electric drill",
  "Screwdriver set"],
  "commit"=>"Go!"}

But controller create method keeps making next error:

undefined method `each' for nil:NilClass` on the `params[:item_name]

from controller:

def create
    params[:item_name].each do |item|
      item = @signup_parent.inventories.build(inventory_params)
    end
end

def inventory_params
  params.require(:inventory).permit(:signup_id, {:item_name => []})
end

UPDATED controller code (see commentary thread below, still not working, but hopefully getting closer)

def create
    params[":item_name"].each do |item|
      item = @signup_parent.inventories.build(inventory_params)
    end
end

def inventory_params
  params.permit(:signup_id, {:item_name => []})
end

View code snippet in case it's helpful:

<%= check_box_tag ":item_name[]", "#{thing}" %>
<%= hidden_field_tag :signup_id, "#{Signup.find_by_email(session[:signup_email]).id}" %>

Upvotes: 2

Views: 131

Answers (1)

Alexander Kireyev
Alexander Kireyev

Reputation: 10825

You have ":item_name" (with quotes and colon) param, so i guess you should use it with quotes:

params[":item_name"].each do |item|
...

And better handle how it pass from particular form.

There is also a problem with inventory_params - there is no inventory that you can require, they are all in root. So i think this:

params.require(:inventory).permit(:signup_id, {:item_name => []})

won't work. You can use normal params, or handle this on the frontend. However you can permit them without require:

params.permit(:signup_id, {':item_name' => []})

or:

params.permit!

Update: you should change your files to

<%= check_box_tag "inventory[item_name][]", "#{thing}" %>
<%= hidden_field_tag "inventory[signup_id]", "#{Signup.find_by_email(session[:signup_email]).id}" %>

Then params change so you could use params.permit(:signup_id, {:item_name => []})

Upvotes: 1

Related Questions