This project is archived and is in readonly mode.
accepts_nested_attributes_for bug?
-
kylc
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
- 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.beta4When 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
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
- 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
@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
- State changed from new to invalid
- Importance changed from to Low
fields_fornot rendering anything if no object(s) are built is the intended behavior.

