This project is archived and is in readonly mode.
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.
-
josh August 19th, 2008 @ 05:43 AM
- State changed from new to open
- Assigned user set to josh
+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} )
-
csnk May 18th, 2011 @ 08:23 AM
We are the professional suits manufacturer, suits supplier, suits factory, custom suits.
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="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>
People watching this ticket
Attachments
Tags
Referenced by
- 853 Fix render :partial to not attempt to render collections that are nil This is also included in my patch for ticket #791.
- 791 Add empty_template option when rendering a collection (from [a8ece12fe2ac7838407954453e0d31af6186a5db]) Return ...