0

Here's the code:

= form_for @form do |f|
  = f.fields_for :questions do |q|
    %p
      = q.object.content
      = q.fields_for :answers do |a|
        %p= a.text_area :content

What the name attribute on the text_area should be form[questions_attributes][0][answer_attribute][content] but it's showing. form[questions_attributes][0][answers][content].

Here's my models.

# answer.rb
  belongs_to :question

# question.rb
  has_one :answer
  accepts_nested_attributes_for :answer

# form.rb
  has_many :questions, :order => 'position ASC'
  accepts_nested_attributes_for :questions

So what I'm getting in the log is WARNING: Can't mass-assign protected attributes: answers

Any help would be greatly appreciated. Thanks!

update

here's the log so you can see what's being passed:

Started POST "/forms/16" for 127.0.0.1 at Fri Mar 09 16:53:58 -0500 2012
  Processing by FormsController#update as HTML
  Parameters: {"commit"=>"Update Form", "utf8"=>"✓", "id"=>"16", "authenticity_token"=>"mcRJP8XgvE0Cl1JsPryER47+Hbx5DwpEveR1m0R7S6k=", "form"=>{"opportunity_id"=>"1", "questions_attributes"=>{"0"=>{"id"=>"101", "answers"=>{"content"=>"asdfasdf"}}, "1"=>{"id"=>"102", "answers"=>{"content"=>"asdfasdf"}}, "2"=>{"id"=>"103", "answers"=>{"content"=>"asdfasdf"}}, "3"=>{"id"=>"104", "answers"=>{"content"=>""}}, "4"=>{"id"=>"105", "answers"=>{"content"=>""}}, "5"=>{"id"=>"106", "answers"=>{"content"=>""}}, "6"=>{"id"=>"107", "answers"=>{"content"=>""}}, "7"=>{"id"=>"108", "answers"=>{"content"=>""}}}, "status"=>"Not Reviewed", "current_step"=>"", "account_id"=>"1"}}
  SQL (0.8ms)   SELECT name
 FROM sqlite_master
 WHERE type = 'table' AND NOT name = 'sqlite_sequence'

  Form Load (0.2ms)  SELECT "forms".* FROM "forms" WHERE "forms"."id" = 16 LIMIT 1
  Question Load (1.1ms)  SELECT "questions".* FROM "questions" WHERE "questions"."id" IN (101, 102, 103, 104, 105, 106, 107, 108) AND ("questions".form_id = 16) ORDER BY position ASC
WARNING: Can't mass-assign protected attributes: answers
WARNING: Can't mass-assign protected attributes: answers
WARNING: Can't mass-assign protected attributes: answers
WARNING: Can't mass-assign protected attributes: answers
WARNING: Can't mass-assign protected attributes: answers
WARNING: Can't mass-assign protected attributes: answers
WARNING: Can't mass-assign protected attributes: answers
WARNING: Can't mass-assign protected attributes: answers
Redirected to http://vol.dev/forms/16
Completed 302 Found in 208ms

Update 2

When I add answers to attr_accessible in question.rb I get ActiveRecord::UnknownAttributeError (unknown attribute: answers)

3 Answers 3

1

I left everything in my models as is but I did this to the form:

 = f.fields_for :questions do |q|
    = q.fields_for :answer, q.object.answer do |a|
      = a.hidden_field :question_id, :value => q.object.id
      = a.hidden_field :form_id, :value => @form.id
      %p
        = a.label :content, q.object.content
        %br
        = a.text_area :content

And it works perfectly. If there's any refining or anything feel free to comment on this.

Sign up to request clarification or add additional context in comments.

Comments

0

You need to have attr_accessible for questions in form.rb

attr_accessible :questions_attributes

You might have to add all other fields in form.rb too to attr_accessible.

1 Comment

Already have that in my form.rb. So that's not it unfortunately. Thanks.
0

Had the same issue on my end Marc.

The two reasons why this was not working for you are:

  1. Your association was a has_one between question and answer so you need to use the singular version in your fields_for argument:

    q.fields_for :answer

  2. You need to build a answer for the question (in your controller, preferably):

    question.build_answer

You ended up doing these in your new form code that you posted as an answer but I thought I would explain why it worked.

Cheers,

JP

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.