2012-03-09 89 views
0

下面的代码:嵌套属性的形式呈现不属性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添加answersattr_accessible我得到ActiveRecord::UnknownAttributeError (unknown attribute: answers)

回答

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 

而且它完美的作品。如果有任何改进或任何东西可以随意对此发表评论。

0

您需要在form.rb中有attr_accessible问题

attr_accessible :questions_attributes 

您可能需要将form.rb中的所有其他字段也添加到attr_accessible。

+0

已经有我的'form.rb'。所以那不太可惜。谢谢。 – Marc 2012-03-09 21:53:34

0

对我的结局Marc有同样的问题。

为什么这不是为你工作的两个原因是:

  1. 你的公会是questionanswer之间的has_one,所以你需要在你的fields_for参数使用单一版本:

    q.fields_for :answer

  2. 你需要建立一个问题的答案(在你的控制器,最好):

    question.build_answer

你结束了在你张贴的答案你的新形式的代码做这些,但我想我会解释为什么工作。

干杯,

JP