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)。

+0

我不认为你需要定义location_attributes astr_accessible – usha

+0

愚蠢的想法,但服务器重新启动? ;) –

+0

而且 - 哪个rails版本是? –

回答

0

这实际上结束了与控制器更新操作,这是使用管理标志的一个问题 -

if user.update_attributes(params[:user], as: :admin) 

- 针对特定的应用程序,原因。奇怪的是,如果我拿走了那面旗帜,它的效果很好。所以我不得不找出一种方法来解决它 - 如果位置正在更新以及不更新管理。以防万一有人遇到类似的事情。