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.

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

#509

Reported by Simon Jefford · June 29th, 2008 @ 10:47 PM

State: resolved
Milestone: 2.x
Assigned to: Pratik Pratik
Importance: none

Activity

  1. Simon Jefford
    Simon Jefford

    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"

    June 29th, 2008 @ 10:49 PM

  2. Steven Bristol
    Steven Bristol

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

    blah ||= item

    at the top of your partial?

    June 30th, 2008 @ 03:07 PM

  3. Simon Jefford
    Simon Jefford
    • 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.

    June 30th, 2008 @ 03:34 PM

  4. Steven Bristol
    Steven Bristol

    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.

    June 30th, 2008 @ 03:47 PM

  5. Simon Jefford
    Simon Jefford

    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!

    June 30th, 2008 @ 04:42 PM

  6. DHH
    DHH

    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.

    July 2nd, 2008 @ 05:01 AM

  7. Pratik
    Pratik
    • Assigned user set to Pratik

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

    Thanks.

    July 2nd, 2008 @ 05:29 AM

  8. Repository
    Repository

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

    http://github.com/rails/rails/co...

    July 2nd, 2008 @ 04:42 PM

  9. Pratik
    Pratik
    • State changed from new to resolved

    Committed after making several changes.

    Thanks.

    July 2nd, 2008 @ 04:42 PM

  10. bingbing