Note that there are some explanatory texts on larger screens.

plurals
  1. PORails 3.1: accepts_nested_attributes_for and has_one association - won't work?
    primarykey
    data
    text
    <p>I'm trying to use accepts_nested_attributes_for on a has_one association model, and getting absolutely nowhere :-(</p> <p>I have two models, a user and a location. A user has one location:</p> <pre><code>class User &lt; ActiveRecord::Base # current location has_one :location, :dependent =&gt; :destroy accepts_nested_attributes_for :location end class Location &lt; ActiveRecord::Base belongs_to :user end </code></pre> <p>I can save changes to the model by using <code>User.find(1).location.current_location_text = "blah"</code> from the console, so I know the associations are set up correctly.</p> <p>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":</p> <pre><code>&lt;%= form_for(@user) do |f| %&gt; &lt;%= fields_for(@user.location) do |location_fields| %&gt; &lt;%= location_fields.label :current_location_text, 'Current Location' %&gt; &lt;%= location_fields.text_field :current_location_text, :placeholder =&gt; 'Road, City or Postcode' %&gt; &lt;% end %&gt; &lt;%= f.submit "Update Current Location" %&gt; &lt;% end %&gt; </code></pre> <p>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:</p> <pre><code>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"=&gt;"✓", "authenticity_token"=&gt;"YdTAsXwEvRgXIqri+jfx3dLlYG2XWQTuYkgLDsO/OJw=", "location"=&gt;{"current_location_text"=&gt;"E14 8JS"}, "commit"=&gt;"Update Current Location", "id"=&gt;"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 </code></pre> <p>Two things that I find bizarre about this:</p> <ol> <li><p>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.</p></li> <li><p>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: </p> <pre><code> {"utf8"=&gt;"✓", "authenticity_token"=&gt;"YdTAsXwEvRgXIqri+jfx3dLlYG2XWQTuYkgLDsO/OJw=", "user"=&gt;{"location"=&gt;{"current_location_text"=&gt;"E14 8JS"}, "commit"=&gt;"Update Current Location", "id"=&gt;"1"}} </code></pre></li> </ol> <p>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!</p> <p>Also if it helps, here's my update within my UsersController:</p> <p>def update @user = User.find(params[:id])</p> <pre><code>if @user.update_attributes(params[:user]) redirect_to current_user, :notice =&gt; 'User was successfully updated.' else render :action =&gt; "edit" end </code></pre> <p>end</p> <p>and here's what's in my routes.rb (although I don't think it's relevant):</p> <pre><code>resources :users do resource :location end </code></pre> <p>Any help appreciated. If I don't solve this, the laptop is going out the window.... Thanks.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload