0

In my app, users have one location, and locations belong to users.

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 (users are auto-created, so there is no "new" view)

def edit
  @user = current_user
  @user.build_location
end

users/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

The error I'm getting is "Can't Mass Assign Protected Attributes: country, state, city, address, zipcode."

I've gotten the "Can't Mass Assign Protected Attributes: location_attribute" type of error before, but that's not the problem here.

Here are my (abridged) 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"}

Any idea what's going on here? I've searched for a WHILE, and can't seem to find anyone with this problem (everyone else has the latter mass-assign one).

Community
  • 1
  • 1
Sasha
  • 6,224
  • 10
  • 55
  • 102

1 Answers1

0

This actually ended up being a problem with the update action in the controller, which was using an admin flag --

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

-- for app-specific reasons. Strangely, if I took that flag away, it worked fine. So I had to figure out a way to work around it -- by not admin updating if location was being updated as well. Just in case anyone's running into something similar.

Sasha
  • 6,224
  • 10
  • 55
  • 102