2012-04-20 54 views
3

我有2个模型。会员和调查如何正确配置rails 3的嵌套属性

member.rb如下

Class Member < ActiveRecord::Base 
    has_one :survey, :dependent => :destroy 
    accepts_nested_attributes_for :survey 

    attr_accessible :fname,:lname, :address, :city, :state, :zip, :email, :phone, :phone_alt, :e_contact, :e_contact_phone, :physician, :physician_phone, :chiropractor, :chiropractor_phone, :password, :password_confirmation, :remember_me, :survey_attributes 

end 

survey.rb但是如下

Class Survey < ActiveRecord::base 
    belongs_to :member 
end 

,每当我试图与调查属性我收到

ActiveModel::MassAssignmentSecurity::Error: Can't mass-assign protected attributes: surveys

创建成员

我正在通过控制台测试这个。

+0

您是否尝试发布实际表单? – 2012-04-20 22:21:08

+0

也许它是:在attr_accessible中的surveys_attributes? – tmaximini 2012-04-20 22:22:54

+0

是的,实际的表单产生相同的输出,并且去除surveys_attributes不会改变输出。 – Kosmonaut 2012-04-20 22:50:45

回答

2

随着has_one协会访问调用应为:

attr_accessible :survey_attributes 

您发布需要PARAMS被嵌套,就像这样:

params = { :member => { :name => 'Jack', :survey_attributes => { :attribute => 'value' } } } 

在形式确保你”重建正确的嵌套关系,即。你必须使用:

= form_for @member do |f| 
    ... 
    = f.fields_for :survey do |s| 
    ... 

如果你有这些东西安装像它应该工作。如果这不是您的错误,那么请在控制台上显示您正在尝试的内容并且无法正常工作。

更多信息,请参见#accepts_nested_attributes_for了Rails的API中。

+0

我的表单正在为调查(调查)生成不正确的属性。将表格更新为survey_attributes,一切正常。谢谢! – Kosmonaut 2012-04-20 23:22:15