Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

fields_for with nested attributes association should accept explicitly passed objects

#1849

Currently when you pass an explicit object to nested attributes' fields_for helper, it will simply be ignored.

This patch yields it as the builder's object in the block.

One could argue whether this is actually needed or not (you can use build outside of the template after all), but for compatibility with the existing fields_for it does make sense to me. Should be a no-brainer.

Reported by Pascal Ehlert · February 2nd, 2009 @ 09:59 PM

State: committed
Milestone: 2.x
Assigned to: Michael Koziarski Michael Koziarski
Importance: none

Activity

  1. Pascal Ehlert
    Pascal Ehlert

    Should have mentioned that this only applies for one-to-one associations..

    February 2nd, 2009 @ 10:09 PM

  2. Eloy Duran
    Eloy Duran

    +1 Applies clean, tests run and looks good to me.

    February 3rd, 2009 @ 12:00 AM

  3. Michael Koziarski
    Michael Koziarski

    Wouldn't this only work with ActiveRecord objects? Not sure if that's a deal breaker here, but seems like we should be able to work with it another way.

    February 3rd, 2009 @ 05:50 AM

  4. Pascal Ehlert
    Pascal Ehlert

    That's true, it works for all objects that respond to "new_record?"..

    You may have noticed that I've been inspired by the existing code here which is why I thought it would be a reasonable assumption.

    I didn't like this too much myself, but I think in order to work with form helpers, attributes should have a #new_record? method anyway.

    Maybe we can just put a note somewhere in the rdoc of form_for?

    February 3rd, 2009 @ 05:59 AM

  5. Eloy Duran
    Eloy Duran

    @Michael: Well ActiveRecord and DataMapper and any objecty which responds to new_record?. (The same kind of stuff happens on line 261 btw.)

    It's not a deal breaker though, we need an ID for new records as well. So since we are already assuming the new_record? method, I chose to use that to check if an object is a record or not.

    February 3rd, 2009 @ 09:50 AM

  6. Repository
    Repository
    • State changed from new to committed

    (from [d15d53cf810014b90827015ecd0e601176492fb7]) Allowing an object to be passed explicitly to a fields_for with nested_attributes on one-to-one associations

    Signed-off-by: Michael Koziarski michael@koziarski.com [#1849 fields_for with nested attributes association should accept explicitly passed... state:committed] http://github.com/rails/rails/co...

    February 6th, 2009 @ 12:48 AM