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.

Rendering a partial with an array as its :object should not render as :collection

#5884

This code does not work the same way as it did in 2.x:

render :partial => "things", :object => ['foo', 'bar', 'baz']

In 2.x, the "things" partial would be rendered once with a local variable called "things" storing the given array.

In 3.0.0 (and edge), the Action View assumes that you really meant :collection => ['foo', 'bar', 'baz'], so it iterates through the array and renders the partial once with each element. It will do this for any object which responds to to_ary.

I really think this is a case of Rails being too clever for its own good. The difference in behaviour between the :object and :collection options is large enough that this makes for a big break in expectations. If someone wants the :collection behaviour, they should use :collection.

(FWIW, an easy workaround is to pass the array into :locals, which people should probably be doing for arrays 99% of the time anyway, but silently assuming they meant :collection is not the way to encourage that behaviour.)

The relevant change to the code is more than a year old now (commit d0301e1) but I couldn't find any discussion about the change and the tests don't seem to assert anything about it either way. If it's really the behaviour we want, then it should be tested and documented. I can make a patch in either case.

-James

With +2 in the core list

Reported by Antonio Tapiador · October 29th, 2010 @ 08:23 AM

State: resolved
Milestone: none
Importance: Low

Activity

  1. Jeff Kreeftmeijer
    Jeff Kreeftmeijer
    • Importance changed from to Low

    Automatic cleanup of spam.

    November 1st, 2010 @ 05:03 PM

  2. Jason King
    Jason King

    I just ran into this myself, thinking here is that this should be fixed, but is low priority? Or is this new behavior intended?

    November 12th, 2010 @ 06:48 PM

  3. Jason King
  4. Neeraj Singh
    Neeraj Singh
    • State changed from new to resolved

    November 12th, 2010 @ 07:46 PM