0
在我的应用程序中,用户有一个位置,位置属于用户。嵌套的属性一对一不同的质量分配错误
user.rb
has_one :location
accepts_nested_attributes_for :location
attr_accessible ... :location_attributes
location.rb
belongs_to :user
attr_accessible :country, :state, :city, :address, :zipcode, :user_id
users_controller(用户自动创建的,所以没有 “新” 观点)
def edit
@user = current_user
@user.build_location
end
个
用户/ edit.html.haml
= form_for @user do |f|
...
= f.fields_for :location do |location|
%p
= location.label :country
= location.text_field :country
%p
= location.label :state
= location.text_field :state
%p
= location.label :city
= location.text_field :city
%p
= location.label :address
= location.text_field :address
%p
= location.label :zipcode
= location.text_field :zipcode
= f.submit
我得到的错误是 “无法大众指定保护属性:国家,州,城市,地址,邮编。”
我已经得到了“Can not Mass Assign Protected Attributes:location_attribute”类型的错误,但这不是问题。
这里是我的(有删节)PARAMS:
{"utf8"=>"✓", "_method"=>"put", "authenticity_token"=>"RTpnMsKuByhnGgs9xrX3d0nzKzcaqIcpS75tsujPX2s=", "user"=>{"name"=>"myname", ... "location_attributes"=>{"country"=>"USA", "state"=>"whatever", "city"=>"", "address"=>"", "zipcode"=>""}}, "commit"=>"Update account", "action"=>"update", "controller"=>"users", "id"=>"219"}
任何想法是怎么回事?我搜索了一个WHILE,似乎无法找到有此问题的人(everyoneelsehasthe latter mass-assign one)。
我不认为你需要定义location_attributes astr_accessible – usha
愚蠢的想法,但服务器重新启动? ;) –
而且 - 哪个rails版本是? –