Note that there are some explanatory texts on larger screens.

plurals
  1. PORails Nested Object Form *_attributes
    primarykey
    data
    text
    <p>I'm using Rails 2.3.2, and trying to get a nested object form to work properly. I've narrowed my problem to the issue that Rails is not setting my nested form elements with the *_attributes required to initiate the accepts_nested_attributes_for processing. </p> <p>My model code is:</p> <pre><code>class Person &lt; Party has_one :name, :class_name =&gt; "PersonName" accepts_nested_attributes_for :name, :allow_destroy =&gt; true end class PersonName &lt; ActiveRecord::Base belongs_to :person end </code></pre> <p>My view code looks like this (I'm using HAML):</p> <pre><code>%h3 New customer = error_messages_for :person, :person_name, :name, :country - form_for :person, :url =&gt; collection_url, :html =&gt; {:class =&gt; 'MainForm'} do |person_form| - @person.build_name unless @person.name - person_form.fields_for :name do |name_form| = name_form.label :given_name, "First Name:" = name_form.text_field :given_name = name_form.label :family_name, "Last Name:" = name_form.text_field :family_name = hidden_field_tag :inviter_id, params[:inviter_id] = hidden_field_tag :inviter_code, params[:inviter_code] %p= submit_tag "Create" = link_to 'Back', collection_url </code></pre> <p>Instead of params being: </p> <pre><code>{"person"=&gt;{"name_attributes"=&gt;{"given_name"=&gt;"Fred", "family_name"=&gt;"Flintstone"}}, ...} </code></pre> <p>I get:</p> <pre><code>{"person"=&gt;{"name"=&gt;{"given_name"=&gt;"Fred", "family_name"=&gt;"Flintstone"}}, ...} </code></pre> <p>As a result, I get a TypeMismatch exception. I've followed the documentation from <a href="http://ryandaigle.com/articles/2009/2/1/what-s-new-in-edge-rails-nested-attributes" rel="nofollow noreferrer">Ryan Daigle</a>. I've also followed the advice from <a href="http://www.pixellatedvisions.com/2009/03/18/rails-2-3-nested-model-forms-and-nil-new-record" rel="nofollow noreferrer">this blog</a> and the <a href="https://github.com/ryanb/complex-form-examples/tree" rel="nofollow noreferrer">complex-forms-example</a>. </p> <p>Using Firebug, I went through my form and adjusted the name attribute of the input tags from name to name_attributes. This produced the params with name_attributes, and the create worked fine.</p> <p>I'm stuck as I cannot figure out why my form is not producing the *_attributes form of the name.</p> <p>Another thing I tried is I got the complex_form_example working in my environment. I've gone through every inch of the controller, models and views and compared it to my code. I cannot find what is different. I know this is something small, and would appreciate any help!</p> <p>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.
 

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