This project is archived and is in readonly mode.

#509 ✓resolved
Simon Jefford

:variable_name option added to render :partial .. :collection

Reported by Simon Jefford | June 29th, 2008 @ 10:47 PM | in 2.x

Comments and changes to this ticket

  • Simon Jefford

    Simon Jefford June 29th, 2008 @ 10:49 PM

    This patch adds a :variable_name option for use when rendering collections using partials. It allows you to do something like:

    render :partial => 'some_partial_name', :collection => @people, :variable_name => "person"

  • Steven Bristol

    Steven Bristol June 30th, 2008 @ 03:07 PM

    If your partial is called _item, why not just do:

    blah ||= item

    at the top of your partial?

  • Simon Jefford

    Simon Jefford June 30th, 2008 @ 03:34 PM

    • Tag changed from actionpack to actionpack, patch

    Because that line isn't going to mean much to the reader and will just add noise. Say I have a partial for product images that is for admin pages only - it contains controls that allow you to delete the image. A logical name for the partial would be _admin_product_image. So, using your scheme you would have:

    product_image ||= admin_product_image

    I can't really see any compelling reason for doing that over :variable_name.

  • Steven Bristol

    Steven Bristol June 30th, 2008 @ 03:47 PM

    For anyone who can read ruby and knows the rails api, this is not really noise. IMO.

    Doing it your way means you would have to add that variable everywhere you called the partial. In your example that doesn't seem to matter, but it condones a bad practice. Encapsulation and DRY would suggest pushing what amounts to a config variable do the stack as far as possible.

    Your example might be better served if you create an admin namespace and put the partial in there. In which case the partial is still named product_image.

  • Simon Jefford

    Simon Jefford June 30th, 2008 @ 04:42 PM

    You make a good point with regards to DRY. I think I now agree - this patch isn't all that. What can I say - it seemed like a good idea late last night!

  • DHH

    DHH July 2nd, 2008 @ 05:01 AM

    I think it's a great idea, Simon! I've been wanting this for a long time. I'd just suggest using :as instead of :variable_name. Thanks for working on this.

  • Pratik

    Pratik July 2nd, 2008 @ 05:29 AM

    • Assigned user set to “Pratik”

    Can you also add this to ActionView::Base#render and tests ?


  • Repository

    Repository July 2nd, 2008 @ 04:42 PM

    (from [2b43620e3c1352028f19550fcde4632d65cbd191]) Add :as option to render a collection of partials with a custom local variable name. [#509 state:resolved] [Simon Jefford, Pratik Naik]

  • Pratik

    Pratik July 2nd, 2008 @ 04:42 PM

    • State changed from “new” to “resolved”

    Committed after making several changes.


  • bingbing

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=""></a>