This project is archived and is in readonly mode.
Add empty_template option when rendering a collection
-
RSL
+1 especially with the option for handling simple one line HTML strings [as discussed in #rails-contrib].
-
Frederick Cheung
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
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
BTW, a discussion of this can be found at this thread.
-
Ryan Bates
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
Correction: you'll need to wrap the render call in parenthesis.
<%= render(:partial => @records) || "Nothing found" %> -
Eloy Duran
+1 for Ryans patch. Seems the best Ruby-ish solution.
-
RSL
What about the case where you want something more complex than a simple string rendered when the collection is empty?
-
Damian Janowski
RSL,
<%= render(:partial => @records) || render_very_complex_logic_found_in_helpers__of_course %> -
Damian Janowski
Ryan, why not checking for collection.blank? so that it doesn't fail with nils? Other than that I'm +1.
-
Ryan Bates
@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
Nevermind, found the problem. Attached is an updated patch which also returns nil when nil is passed.
-
Damian Janowski
Great :-)
Tests pass and there's no need for doc changes.
+1
-
RSL
@ ryan & damien, doy. where is my mind? i like this.
-
S. Brent Faulkner
+1 for render_empty_collection_or_nil_returns_nil.diff
definitely.
-
Repository
- 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 Add empty_template option when rendering a collection state:resolved]
Signed-off-by: Joshua Peek josh@joshpeek.com http://github.com/rails/rails/co...
-
Andrew Vit
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} )
