Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nested form & update_attributes

i am having trouble updating data in a multi-level nested form. i use partials to include all the fields for both the create & update views, and i do NOT have a problem with creating. only with updating.

essentially the structure (simplified) is:

user has_one profile
profile has_many addresses

form_for @user do |u|
  u.fields_for :profile do |p|
    p.fields_for :addresses do |a|

like i said, creating the user, profile, and addresses works fine. only until i attempt to update do i find problems. i don't receive an error, it actually shows it was successfully updated. and it actually does properly update the user & profile fields, just not the address fields.

here are the params for the update from the stack trace. (again, summarized & formatted)

Parameters: {"controller"=>"profiles", "action"=>"update", "_method"=>"put", "id"=>"1", 
  "user"=>{"login" => "username",
    "profile_attributes"=>{"first_name"=>"Admin",
      "addresses_attributes"=>{
        "0"=>{"address"=>"123 Address Ave.", "city"=>"Cityville", "state"=>"CA"}
      }
    }
  }
}

all of the documentation i can find only shows 1 nested form, so i am not sure if i am using update_attributes properly for more than 1 level deep.

any thoughts?

like image 877
brewster Avatar asked Dec 16 '22 21:12

brewster


1 Answers

Are you using attr_accessible anywhere in your model, to whitelist the fields that are allowed for mass assignment? If so, then you'll also need to add

attr_accessible :address_attributes

to allow those attributes to be passed to update_attributes.

If you aren't already using attr_accessible (or it's not-recommended sister attr_protected) then don't add this line as it'll stop all of your ther attributes being saved.

like image 93
Gareth Avatar answered Dec 29 '22 18:12

Gareth