0

I'm trying to update a nested model with a simple has_many / belongs_to association

I've setup parameters in the controller with

params.require(:survey).permit(:name, :questions[[:id, :content]])

but I get the No implicit conversion of type Array to Integer

console dump below. From similar issues I've read the problem may be how this :questions_attributes is hashed - no idea where this needs to be fixed tho - any ideas appreciated!

Thanks

Started PATCH "/surveys/1" for 127.0.0.1 at 2014-01-04 15:53:31 +1100
Processing by SurveysController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"l5ANLS/y2Z+aB4xiJzEw+pF+j7V7LQk4THfU7mkTGX4=", "survey"=>{"name"=>"do u like cats?", "questions_attributes"=>{"0"=>{"content"=>"nope3", "id"=>"1"}, "1"=>{"content"=>"no way", "id"=>"2"}}}, "commit"=>"Update Survey", "id"=>"1"}
  Survey Load (0.3ms)  SELECT "surveys".* FROM "surveys" WHERE "surveys"."id" = $1 LIMIT 1  [["id", "1"]]
Completed 500 Internal Server Error in 2ms

TypeError (no implicit conversion of Array into Integer):
  app/controllers/surveys_controller.rb:72:in `[]'
  app/controllers/surveys_controller.rb:72:in `survey_params'
  app/controllers/surveys_controller.rb:44:in `block in update'
  app/controllers/surveys_controller.rb:43:in `update'

Update: This is what is posted when :question_attributes does not have extra parentheses

{"utf8"=>"✓",
 "_method"=>"patch",
 "authenticity_token"=>"l5ANLS/y2Z+aB4xiJzEw+pF+j7V7LQk4THfU7mkTGX4=",
 "survey"=>{"name"=>"do u like cats?",
 "questions_attributes"=>{"0"=>{"content"=>"nope2",
 "id"=>"1"},
 "1"=>{"content"=>"no way",
 "id"=>"2"}}},
 "commit"=>"Update Survey",
 "id"=>"1"}
MikeW
  • 4,749
  • 9
  • 42
  • 83

2 Answers2

1

just replace :questions[[:id, :content]] with

:questions_attributes => [:id, :content]
medBouzid
  • 7,484
  • 10
  • 56
  • 86
  • If you re-read my answer, I had already given you that. I didn't just correct your double braces. – Jon Jan 04 '14 at 10:20
0

Your params permit statement is not quite right.

Assuming you're using accepts_nested_attributes_for in your model and fields_for in your form, you'll need to update your permit statement as follows:

params.require(:survey).permit(:name, questions_attributes: [:id, :content])

If you want to be able to delete child objects through the form too, you'll need to add :_destroy into the questions_attributes array too.

Jon
  • 10,678
  • 2
  • 36
  • 48
  • that was a typo on my part with the double braces. I was attempting to wrap the attributes in [] , so I have it exactly as in your answer. I think somehow I need to make the params wrap the questions_attribute in a certain way. I updated my answer with current params that are sent in the request – MikeW Jan 04 '14 at 05:22
  • Please also note that `question_attributes: []` is equivalent `:question_attributes => []` – Jon Jan 04 '14 at 10:29
  • I believe its :questions_attributes (as marked) not questions_attributes: (as in yours) - where the colon sits seems to make a difference. – MikeW Jan 05 '14 at 11:50
  • From Ruby 1.9 onwards you can use `:symbol => value` and `symbol: value` interchangeably. Take a look at this question and its answer for more details - http://stackoverflow.com/questions/18835189/ruby-rails-hash-rockets-syntax – Jon Jan 05 '14 at 22:07