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.

[PATCH] When a collection rendered is empty it's now possible to assign to it a default template !

#3187

Until now with Rails when we would handle empty collections in apps, we had to code it like that :

<%# app/views/posts/index.erb %>
<% if @posts.empty? %>
 <p>Sorry, there is no posts yet !</p>
<% else %>
 <% render :partial => "post", :collection => @posts %>
<% end %>

Now with my modest contribution we can handle it in one line in view :

<%# app/views/posts/index.erb %>
<%= render :partial => "post", :collection => @posts, :default_template => "no_post" %>

<%# app/views/posts/_no_post.erb %>
<p>Sorry, there is no posts yet !</p>

So we can implement Getting real notion called "The blank slate" more easily.

Thanks.

Reported by Joseph ROUFF · September 11th, 2009 @ 08:40 AM

State: wontfix
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. Jeremy Kemper
    Jeremy Kemper
    • State changed from new to wontfix

    Nice patch, but I think the option will obfuscate rather than clarify.

    Simply using a conditional here is appropriate and clear, not verbose or hard.

    September 12th, 2009 @ 11:13 AM

  2. Joseph ROUFF
    Joseph ROUFF

    I understand that you say, really.

    But I think that patche is really useful for people who have to use blank slates a lot in their apps (for instance in widgets page for each widget). The conditional become painful for them.

    Perhaps with a name more explicit and clear it will be better :

    <%= render :partial => "post", :collection => @posts, :if_blank => "no_post" %>
    

    Instead of

    <%= render :partial => "post", :collection => @posts, :default_template => "no_post" %>
    

    (new name option suggested by users of Rails core list )

    What do you think of that ?

    Thanks.

    September 12th, 2009 @ 11:56 AM

  3. Jeremy Kemper
    Jeremy Kemper

    If you repeat this a lot, I suggest wrapping it with a helper method.

    <%= render_widgets @posts %>
    

    September 12th, 2009 @ 01:06 PM

  4. Jarkko Laine
    Jarkko Laine

    -1

    I don't think this adds enough value compared to already working code:

    <%= render :partial => "post", :collection => @posts or render :partial => "no_post" %>
    

    If you disagree, why not make it a plugin and if it gains traction, I'm sure it will increase its odds to get included in the core.

    September 14th, 2009 @ 11:41 AM