以下是代码:
= 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
text_area
上的名称属性应该是form[questions_attributes][0][answer_attribute][content]
,但它正在显示。 form[questions_attributes][0][answers][content]
。
这是我的模特。
# 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
所以我在日志中得到的是WARNING: Can't mass-assign protected attributes: answers
非常感谢任何帮助。谢谢!
这是日志,所以你可以看到传递的内容:
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
当我在answers
中向attr_accessible
添加question.rb
时,我得到ActiveRecord::UnknownAttributeError (unknown attribute: answers)
答案 0 :(得分:1)
我将模型中的所有内容保留原样,但我按照以下形式进行了操作:
= 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
它完美无缺。如果有任何改进或任何事情可以随意发表评论。
答案 1 :(得分:0)
你需要在form.rb
中有问题的attr_accessibleattr_accessible :questions_attributes
您可能还必须将form.rb中的所有其他字段添加到attr_accessible。
答案 2 :(得分:0)
在我的结局Marc上有同样的问题。
这不适合你的两个原因是:
您的has_one
与question
之间的关联为answer
,因此您需要在fields_for
参数中使用单数版本:
q.fields_for :answer
您需要为问题建立答案(最好在您的控制器中):
question.build_answer
你最后在你发布的新表单代码中做了这些,但我想我会解释为什么它起作用。
干杯,
JP