Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm trying to use accepts_nested_attributes_for on a has_one association model, and getting absolutely nowhere :-(

I have two models, a user and a location. A user has one 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 to the model by using User.find(1).location.current_location_text = "blah" from the console, so I know the associations are set up correctly.

I have two forms on the edit user page. One that updates the main user attributes (and works fine and not shown below) and then this one that allows the user to update an attribute of the location model, called "current_location_text":

<%= form_for(@user) do |f| %>  
    <%= fields_for(@user.location) do |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 slightly confused as the params sent by the form look incorrect. When the form is submitted, this is in the log:

Started PUT "/users/1" for 127.0.0.1 at 2011-10-08 00:28:05 +0100
  Processing by UsersController#update as 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`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
  User Load (5.3ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` = ? LIMIT 1  [["id", "1"]]
  SQL (4.4ms)  BEGIN
   (2.5ms)  COMMIT
Redirected to http://localhost:3000/users/1

Two things that I find bizarre about this:

  1. There's the "COMMIT" message, but with no preceding update string, and no error. eg, if you tried to commit a protected attributed, you'd get the "you can't mass assign..." error message at that point.

  2. The params look wrong to me. The "location" bit is nested as I'd expect, but I'd also expect this to be a nested within the "user" hash, something like 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 completely stupid here. Am I missing something really obvious? I've tried adding extra hidden fields to my form, ie a user id, and then I get the user hash, but at the same level as the "location" hash, and not as a parent of it as I'd expect!

Also if it helps, here's my update within my UsersController:

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

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

end

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

resources :users do
  resource :location
end

Any help appreciated. If I don't solve this, the laptop is going out the window.... Thanks.

like image 485
DaveStephens Avatar asked Oct 08 '11 00:10

DaveStephens


1 Answers

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

This is your problem. You need to actually "nest" the fields_for inside your form, like this:

<% f.fields_for(@user.location) do |location_fields| -%>
like image 91
Elliot Nelson Avatar answered Sep 27 '22 17:09

Elliot Nelson