This project is archived and is in readonly mode.

#3187 ✓wontfix
Joseph ROUFF

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

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

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.

Comments and changes to this ticket

  • Jeremy Kemper

    Jeremy Kemper September 12th, 2009 @ 11:13 AM

    • 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.

  • Joseph ROUFF

    Joseph ROUFF September 12th, 2009 @ 11:56 AM

    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.

  • Jeremy Kemper

    Jeremy Kemper September 12th, 2009 @ 01:06 PM

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

    <%= render_widgets @posts %>
    
  • Jarkko Laine

    Jarkko Laine September 14th, 2009 @ 11:41 AM

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

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

Tags

Pages