views:

492

answers:

2

In one form I am creating a territory and editing multiple users. The "user_attributes" below are for the users and the "name" is for the territory. So for each user_attribute I wanted to update the user model.

params

{ "territory"=>{"name"=>"Central Canada",
  "user_attributes"=>[{"user_id"=>"30"},{"user_id"=>"30"}]}
}

create action

@territory = @current_account.territories.new[:territory]
params[:user_attributes].each do |item|
  @user = User.find(item[:user_id])
  @user.update_attribute(:territory_id, @territory.id)
end

But rails is kicking back that params[:user_attributes] is nil. But you can see from the params its not. Am I missing something??

A: 

Try params["user_attributes"].

Ben Alpert
Nope sorry, didn't change anything.
dMix
+6  A: 

From what you posted, your user_attributes hash is INSIDE your territory hash. That should be your problem -- either move it outside, or do params[:territory][:user_attributes]

J Cooper
That was it, it worked. I had originally tried that but it didn't work. It turned out that the @user.update_attribute line wasn't working because @territory wasn't saved yet so it wasn't assigned an ID yet.
dMix