This project is archived and is in readonly mode.
:variable_name option added to render :partial .. :collection
-
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"
-
Steven Bristol
If your partial is called _item, why not just do:
blah ||= item
at the top of your partial?
-
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.
-
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.
-
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!
-
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.
-
Pratik
- Assigned user set to Pratik
Can you also add this to ActionView::Base#render and tests ?
Thanks.
-
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]
