This project is archived and is in readonly mode.

#5337 ✓invalid
Matt Simpson

accepts_nested_attributes_for bug?

Reported by Matt Simpson | August 9th, 2010 @ 05:59 AM

I have a new model object
with accepts_nested_attributes_for to an associated model
When I go to the html form, the fields for those nested attributes are not rendered

But, if I have a model object where that associated model object is saved, the form is rendered with the correct data.

Is there anything I need to do to get the form fields for the nested attributes render when no data exists? Or is this method only for associated objects already created?

I've attached screenshots. 1 has data and renders the form, the other does not.

Models:

class Booking < ActiveRecord::Base
  has_one :event_address, :as => :addressee

  accepts_nested_attributes_for :event_address
end

class EventAddress < ActiveRecord::Base
  belongs_to :addressee, :polymorphic => true
end

Haml:

= form_for @booking, :url => booking_event_information_path(@booking) do |f|
  = f.error_messages
  
  = f.fields_for :event_address do |f2| 
    = f2.label :address_1, "Address"
    = f2.text_field :address_1
    %br
  
    = f2.label :address_2
    = f2.text_field :address_2
    %br
  
  = f.submit "Save event information"

Any help is much helpful :) Thanks.

Comments and changes to this ticket

  • kylc

    kylc August 9th, 2010 @ 09:03 PM

    Make sure that you're building the nested model in the controller before rendering the form:

    def new
      @booking = Booking.new
      @booking.addressee.build
    end
    
  • Matt Simpson

    Matt Simpson August 10th, 2010 @ 04:38 AM

    • Tag changed from active record, activerecord associations, nested attributes, activerecord to active record, activerecord associations, nested attributes, rails 3.0.0.beta4, activerecord

    I'm terribly sorry, I forgot to mention this is in Rails 3:

    [msimpson@dakota Code] (master) $ ruby -v
    ruby 1.9.2dev (2010-07-02 revision 28524) [x86_64-darwin10.4.0]
    [msimpson@dakota Code] (master) $ rails -v
    Rails 3.0.0.beta4
    

    When I call the build method on the end of either @booking.event_address or @booking.addressee I get:

    NoMethodError (undefined method `build' for nil:NilClass):
      app/controllers/booking_controller.rb:17:in `event_information'
    

    Thanks

  • Matt Simpson

    Matt Simpson August 10th, 2010 @ 04:43 AM

    However, looking further into the documentation (note that build is not in the method list) the correct usage is:

    @booking.build_event_information and doing so does render the form. Thank you, is this by design for the accepts_nested_attributes_for method? If I do not have accepts_nested_attributes_for set the fields do render, just not with the functionality needed. Perhaps this should be in the documentation for accepts_nested_attributes_for?

    Thank you for your help and direction.

  • Rohit Arondekar

    Rohit Arondekar October 7th, 2010 @ 05:21 AM

    • Tag changed from active record, activerecord associations, nested attributes, rails 3.0.0.beta4, activerecord to active record, nested attributes, rails 3.0.0.beta4, activerecord, associations
  • Aditya Sanghi

    Aditya Sanghi October 7th, 2010 @ 08:48 AM

    @matt, just chiming in here as a fellow user.

    Yes this is the intended functionality. When you are using accepts_nested_attributes_for, the association needs to be built before the form can be rendered. The method used for building does depend on the type of association you have (has_many or belongs_to etc) in the base model and is not really connected to the attribute_nested_for issue.

    If you do not use accepts_nested_attributes_for, the keys in received from the form params in your model differ to when you do use accepts_nested_attributes_for in your base model differ and you have to manually write the plumbing in your model (or elsewhere) to ensure the association is built and saved.

    Ryan has a set of Railscasts on Complex forms which explain the functionality very well. I think there is a Rails Guide on Nested form in progress which when released would serve as authoritative documentation on the feature.

  • David Trasbo

    David Trasbo October 7th, 2010 @ 08:56 PM

    • State changed from “new” to “invalid”
    • Importance changed from “” to “Low”

    fields_for not rendering anything if no object(s) are built is the intended behavior.

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Pages