#3187 : Handle "blank slate" more easily with render method thanks to :default_template option

Hi,

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.

One of you can review my patch (to see if i forget something) or comment it ?

Thanks.

I like the concept, not the name. Can I suggest something like:

render :partial => "post", :collection => @posts, :if_nil => "no_post"

??

The concept is great! Maybe go :if_blank since that's the check you
are running on it.

Adam

:if_nil doesn't describe all cases. The collection could be not nil (e.g. an empty array) and you'd still want the blank slate to show.

I'd suggest something related to blankness since it's the "blank slate" and both .blank? and nil.blank? return true.

render :partial => "post", :collection => @posts, :if_blank => "none"

-Trek

Hi rouffj,

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.

Best, jeremy

Thanks guys for your suggestions. I Think that i'll adopt :if_blank option name.

Thanks again.

Anyone has other ideas ?

I like the idea of being able to render a collection in one line and I too find it a bit clunky to include the conditional and literal content for the blank condition alongside the 99% case.

+1 for :if_blank

-Chris

I liked the idea. I was just wondering if ":when_blank =>" would not fit a bit better than ":if_blank". Not that it really changes nothing and I would still like the :if_blank option, but just an idea...

You can already do this in one line:

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

And soon you'll be able to do:

<%= render @posts or render 'no_post' %>

Which is about as concise as one could want!

Mateo Murphy i think that your code proposal is a little bit less clear and more difficult to read.

It's for that i created a patch.

*For those who would like this to see this patch integrated to rails could you update the lighthouse ticket with +1.*

lighthouse ticket : #3187 [PATCH] When a collection rendered is empty it's now possible to assign to it a default template ! - Ruby on Rails - rails

Thanks for your feedbacks.

Mateo suggests a plain Ruby shorthand since you don't like the obvious, concise Ruby conditional. Not liking this shorthand is not cause for some even more obscure render option.

The ticket was set to "wontfix" already. If you like the feature, please pursue it as a plugin.

Best, jeremy

Ok, thanks for all.

Sorry if i was a bit persistent. It was my first contribution.

I hope that i could help on other tickets on rails core.

Subject closed.