Add empty_template option when rendering a collection
Reported by Frederick Cheung | August 9th, 2008 @ 03:34 PM | in 2.x
I'm sick of writing
@@@ lang=ruby <% if search_results.empty? %> Nothing found <% else %> <%= render :partial => 'search_result', :collection => search_results %> <% end %>
As far as I'm concerned the less actual code and branching in views, the better. Instead I propose
<%= render :partial => 'search_result', :collection => search_results, :empty_template => 'nothing_found' %>
With the nothing_found partial containing the obvious. (I'd rather have a better option name than empty_template but I haven't thought of one so far)
Comments and changes to this ticket
-
RSL August 13th, 2008 @ 01:54 PM
+1 especially with the option for handling simple one line HTML strings [as discussed in #rails-contrib].
-

Frederick Cheung August 13th, 2008 @ 08:09 PM
Revised version of the patch. use cases now include
render :partial => 'record', :collection => @records, :empty => 'Nothing found' render :partial => 'record', :collection => @records, :empty => {:partial => 'nothing_found'} render :partial => 'record', :collection => @records, :empty => {:inline => '<%= @something %>'} -
Steven Bristol August 14th, 2008 @ 01:09 PM
I like this concept a lot, but I am wondering if it might not be better to have render take a block that is used if the collection is empty? I'm not sure that makes sense for the other uses of render, but I prefer that syntax over supplying html as a string.
-
-
Ryan Bates August 15th, 2008 @ 04:40 PM
In this thread some have come to the conclusion that it would be best if "render :collection" returned nil instead of a space for an empty array. This way you can do this with a simple "or" operation.
<%= render :partial => @records || "Nothing found" %>Attached is a patch which does this. I moved some stuff around, so that whenever you're trying to render nil, a space is used in the response body so Safari doesn't complain. This way it happens later on in the response chain so "render :collection" doesn't have to be concerned with it.
-
Ryan Bates August 15th, 2008 @ 04:49 PM
Correction: you'll need to wrap the render call in parenthesis.
<%= render(:partial => @records) || "Nothing found" %> -
-
RSL August 15th, 2008 @ 06:05 PM
What about the case where you want something more complex than a simple string rendered when the collection is empty?
-
Damian Janowski August 15th, 2008 @ 06:17 PM
RSL,
<%= render(:partial => @records) || render_very_complex_logic_found_in_helpers__of_course %> -
Damian Janowski August 15th, 2008 @ 06:19 PM
Ryan, why not checking for collection.blank? so that it doesn't fail with nils? Other than that I'm +1.
-
Ryan Bates August 15th, 2008 @ 06:39 PM
@RSL, as Damian mentioned you can toss it into a helper. Or you can just render a partial right here.
<%= render(:partial => @records) || render(:partial => 'empty') %>@Damian, I tried this briefly but it still errors out with nil. I don't think the fix is that simple so it should probably go in its own ticket.
-
Ryan Bates August 15th, 2008 @ 07:08 PM
Nevermind, found the problem. Attached is an updated patch which also returns nil when nil is passed.
-
Damian Janowski August 15th, 2008 @ 07:34 PM
Great :-)
Tests pass and there's no need for doc changes.
+1
-
-
S. Brent Faulkner August 15th, 2008 @ 09:53 PM
+1 for render_empty_collection_or_nil_returns_nil.diff
definitely.
-
Joshua Peek August 19th, 2008 @ 05:43 AM
- → Assigned user changed from to Joshua Peek
- → State changed from new to open
+1 to returning just returning nil
-

Repository August 20th, 2008 @ 01:09 AM
- → State changed from open to resolved
(from [a8ece12fe2ac7838407954453e0d31af6186a5db]) Return nil instead of a space when passing an empty collection or nil to 'render :partial' [#791 state:resolved]
Signed-off-by: Joshua Peek josh@joshpeek.com http://github.com/rails/rails/co...
-
Andrew Vit November 6th, 2008 @ 10:31 AM
Just a note, this change crashes XML builder templates with empty collections which use this convention:
@items = [] xml << render( :collection => @items )This is because Builder's << operator won't accept nil, and it throws a "can't convert nil to String" error. The easiest workaround seems to be as above, using the || operator with an empty string, but that sure looks funky:
xml << ( render(:collection => @items) || '' )Or else pass in a :locals option and don't use the << convention:
render( :collection => @items, :locals => {:x => xml} )
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".
