kush
kush

Reputation: 16918

Rails: For each Array giving Error?

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??

Upvotes: 1

Views: 2306

Answers (2)

J Cooper
J Cooper

Reputation: 17051

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]

Upvotes: 6

Sophie Alpert
Sophie Alpert

Reputation: 143114

Try params["user_attributes"].

Upvotes: -2

Related Questions