: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 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 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 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 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 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 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 July 2nd, 2008 @ 05:29 AM
- → Assigned user changed from to Pratik
Can you also add this to ActionView::Base#render and tests ?
Thanks.
-

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 July 2nd, 2008 @ 04:42 PM
- → State changed from new to resolved
Committed after making several changes.
Thanks.
Please Login or create a free account to add a new comment.
You can update this ticket by sending an email to from your email client. (help)
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
Source available from github
The Git repository resides at http://github.com/rails
Check out the current development trunk (Edge Rails) with:
git clone git://github.com/rails/rails.git
Creating or reviewing a patch
See the contributor guide.
Creating a feature request
Please don't. If you want a new feature in Rails, you'll have to pull up your sleeves and get busy yourself. Or convince someone else to do it. See the contributor guide on how to get going. But posting them here is just going to lead to ticket root.
Creating a bug report
When creating a bug report, be sure to include as much relevant information as possible. Post the code sample that causes the problem. Preferably, alter the unit tests and show through either changed or added tests how the expected behavior is not occuring.
Security vulnerabilities should be reported via an email to security@rubyonrails.org, do not use trac for reporting security vulnerabilities. All content in trac is publicly available as soon as it is posted.
Then don't get your hopes up. Unless you have a "Code Red, Mission Critical, The World is Coming to an End" kinda bug, you're creating this ticket in the hope that others with the same problem will be able to collaborate with you on solving it. Do not expect that the ticket automatically will see any activity or that others will jump to fix it. Creating a ticket like this is mostly to help yourself start on the path of fixing the problem and for others to sign on to with a "I'm having this problem too".
