Rails Nested Object Form *_attributes problem

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. So instead of
params being:

{“person”=>{“name_attributes”=>{“given_name”=>“Fred”,
“family_name”=>“Flintstone”}}, …}

I get:

{“person”=>{“name”=>{“given_name”=>“Fred”,
“family_name”=>“Flintstone”}}, …}

As a result, I get a TypeMismatch exception. I’ve followed the
documentation from Ryan Daigle. I’ve also followed the advice from this
blog and the complex-forms-example.

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.

I’m stuck as I cannot figure out why my form is not producing the
*_attributes form of the name.

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!

Thanks!

My model code is:

class Person < Party
has_one :name, :class_name => “PersonName”
accepts_nested_attributes_for :name, :allow_destroy => true
end

class PersonName < ActiveRecord::Base
belongs_to :person
end

My view code looks like this (I’m using HAML):

%h3 New customer
= error_messages_for :person, :person_name, :name, :country

  • form_for :person, :url => collection_url, :html => {:class =>
    ‘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

On Apr 13, 7:51 pm, Damon R. [email protected]
wrote:

My view code looks like this (I’m using HAML):

%h3 New customer
= error_messages_for :person, :person_name, :name, :country

  • form_for :person, :url => collection_url, :html => {:class =>
    ‘MainForm’} do |person_form|

    • @person.build_name unless @person.name
    • person_form.fields_for :name do |name_form|

Hi Damon, this is quite an unusual error as the ‘_attributes’ suffix
is hard coded to be appended when the record object responds to symbol

  • ‘_attributes=’ (where symbol in this case is ‘name’). I would check
    to make sure that person_form.object is definitely the @person object.
    Especially as it looks like you are setting @name in the controller
    for the error_messages_for. In fact I think posting the controller
    code would be quite useful if you’re still stuck.

Best regards,
Andrew

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs