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版本是? –