Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Add empty_template option when rendering a collection

#791

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)

Reported by Frederick Cheung · August 9th, 2008 @ 03:34 PM

State: resolved
Milestone: 2.x
Assigned to: josh josh
Importance: none

Activity

  1. RSL
    RSL

    +1 especially with the option for handling simple one line HTML strings [as discussed in #rails-contrib].

    August 13th, 2008 @ 01:54 PM

  2. Frederick Cheung
    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 %>'}
    

    August 13th, 2008 @ 08:09 PM

  3. Steven Bristol
    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.

    August 14th, 2008 @ 01:09 PM

  4. Ryan Bates
    Ryan Bates

    BTW, a discussion of this can be found at this thread.

    August 15th, 2008 @ 03:54 PM

  5. Ryan Bates
    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.

    August 15th, 2008 @ 04:40 PM

  6. Ryan Bates
    Ryan Bates

    Correction: you'll need to wrap the render call in parenthesis.

    
    <%= render(:partial => @records) || "Nothing found" %>
    

    August 15th, 2008 @ 04:49 PM

  7. Eloy Duran
    Eloy Duran

    +1 for Ryans patch. Seems the best Ruby-ish solution.

    August 15th, 2008 @ 05:43 PM

  8. RSL
    RSL

    What about the case where you want something more complex than a simple string rendered when the collection is empty?

    August 15th, 2008 @ 06:05 PM

  9. Damian Janowski
    Damian Janowski

    RSL,

    
    <%= render(:partial => @records) || render_very_complex_logic_found_in_helpers__of_course %>
    

    August 15th, 2008 @ 06:17 PM

  10. Damian Janowski
    Damian Janowski

    Ryan, why not checking for collection.blank? so that it doesn't fail with nils? Other than that I'm +1.

    August 15th, 2008 @ 06:19 PM

  11. Ryan Bates
    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.

    August 15th, 2008 @ 06:39 PM

  12. Ryan Bates
    Ryan Bates

    Nevermind, found the problem. Attached is an updated patch which also returns nil when nil is passed.

    August 15th, 2008 @ 07:08 PM

  13. Damian Janowski
    Damian Janowski

    Great :-)

    Tests pass and there's no need for doc changes.

    +1

    August 15th, 2008 @ 07:34 PM

  14. RSL
    RSL

    @ ryan & damien, doy. where is my mind? i like this.

    August 15th, 2008 @ 07:40 PM

  15. S. Brent Faulkner
    S. Brent Faulkner

    +1 for render_empty_collection_or_nil_returns_nil.diff

    definitely.

    August 15th, 2008 @ 09:53 PM

  16. josh
    josh
    • State changed from new to open
    • Assigned user set to josh

    +1 to returning just returning nil

    August 19th, 2008 @ 05:43 AM

  17. Repository
    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...

    August 20th, 2008 @ 01:09 AM

  18. Andrew Vit
    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} )
    

    November 6th, 2008 @ 10:31 AM