下面的代碼:嵌套屬性的形式呈現不屬性fields_for正確
= 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
的name屬性應爲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
更新2
當我在question.rb
添加answers
到attr_accessible
我得到ActiveRecord::UnknownAttributeError (unknown attribute: answers)
已經有我的'form.rb'。所以那不太可惜。謝謝。 – Marc 2012-03-09 21:53:34