Rails 3.1: accepts_nested_attributes_for and has_one association - won't work? -


i'm trying use accepts_nested_attributes_for on has_one association model, , getting absolutely :-(

i have 2 models, user , location. user has 1 location:

class user < activerecord::base   # current location   has_one :location, :dependent => :destroy   accepts_nested_attributes_for :location end  class location < activerecord::base   belongs_to :user end 

i can save changes model using user.find(1).location.current_location_text = "blah" console, know associations set correctly.

i have 2 forms on edit user page. 1 updates main user attributes (and works fine , not shown below) , 1 allows user update attribute of location model, called "current_location_text":

<%= form_for(@user) |f| %>       <%= fields_for(@user.location) |location_fields| %>         <%= location_fields.label :current_location_text, 'current location' %>         <%= location_fields.text_field :current_location_text, :placeholder => 'road, city or postcode' %>     <% end %>      <%= f.submit "update current location" %> <% end %> 

this doesn't work. i'm confused params sent form incorrect. when form submitted, in log:

started put "/users/1" 127.0.0.1 @ 2011-10-08 00:28:05 +0100   processing userscontroller#update html   parameters: {"utf8"=>"✓", "authenticity_token"=>"ydtasxwevrgxiqri+jfx3dllyg2xwqtuykgldso/ojw=", "location"=>{"current_location_text"=>"e14 8js"}, "commit"=>"update current location", "id"=>"1"}   user load (10.3ms)  select `users`.* `users` `users`.`id` = 1 limit 1   user load (5.3ms)  select `users`.* `users` `users`.`id` = ? limit 1  [["id", "1"]]   sql (4.4ms)  begin    (2.5ms)  commit redirected http://localhost:3000/users/1 

two things find bizarre this:

  1. there's "commit" message, no preceding update string, , no error. eg, if tried commit protected attributed, you'd "you can't mass assign..." error message @ point.

  2. the params wrong me. "location" bit nested i'd expect, i'd expect nested within "user" hash, this:

     {"utf8"=>"✓", "authenticity_token"=>"ydtasxwevrgxiqri+jfx3dllyg2xwqtuykgldso/ojw=", "user"=>{"location"=>{"current_location_text"=>"e14 8js"}, "commit"=>"update current location", "id"=>"1"}} 

i don't think i'm being stupid here. missing obvious? i've tried adding hidden fields form, ie user id, , user hash, @ same level "location" hash, , not parent of i'd expect!

also if helps, here's update within userscontroller:

def update @user = user.find(params[:id])

if @user.update_attributes(params[:user])   redirect_to current_user, :notice => 'user updated.' else   render :action => "edit" end 

end

and here's what's in routes.rb (although don't think it's relevant):

resources :users   resource :location end 

any appreciated. if don't solve this, laptop going out window.... thanks.

<%= fields_for(@user.location) |location_fields| %> 

this problem. need "nest" fields_for inside form, this:

<% f.fields_for(@user.location) |location_fields| -%> 

Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -