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

9 views
Skip to first unread message

rouffj

unread,
Sep 11, 2009, 5:20:11 PM9/11/09
to Ruby on Rails: Core
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.

---
lighthouse ticket : https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/3187
patch : https://rails.lighthouseapp.com/projects/8994/tickets/3187/a/266544/empty_collections_rendering.diff
Message has been deleted

Jason King

unread,
Sep 11, 2009, 8:05:08 PM9/11/09
to rubyonra...@googlegroups.com
I like the concept, not the name. Can I suggest something like:

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

??

Trek Glowacki

unread,
Sep 11, 2009, 10:33:37 PM9/11/09
to rubyonra...@googlegroups.com
: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

Adam Hunter

unread,
Sep 11, 2009, 8:06:38 PM9/11/09
to rubyonra...@googlegroups.com
The concept is great! Maybe go :if_blank since that's the check you
are running on it.

Adam

Jeremy Kemper

unread,
Sep 12, 2009, 6:12:55 AM9/12/09
to rubyonra...@googlegroups.com

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

rouffj

unread,
Sep 12, 2009, 6:15:54 AM9/12/09
to Ruby on Rails: Core
Thanks guys for your suggestions. I Think that i'll adopt :if_blank
option name.

Thanks again.

Anyone has other ideas ?

On Sep 12, 4:33 am, Trek Glowacki <trek.glowa...@gmail.com> wrote:
> :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
>
> >> patch :https://rails.lighthouseapp.com/projects/8994/tickets/3187/a/266544/e...

Chris

unread,
Sep 13, 2009, 8:40:02 AM9/13/09
to Ruby on Rails: Core
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

Rodrigo Rosenfeld Rosas

unread,
Sep 13, 2009, 9:39:17 AM9/13/09
to rubyonra...@googlegroups.com
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...

Em 13-09-2009 12:40, Chris escreveu:
> 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
>
> On Sep 12, 6:15 am, rouffj<rou...@gmail.com> wrote:
>
>> Thanks guys for your suggestions. I Think that i'll adopt :if_blank
>> option name.
>>
>> Thanks again.
>>
>> Anyone has other ideas ?
>>
>> On Sep 12, 4:33 am, Trek Glowacki<trek.glowa...@gmail.com> wrote:
>>

...

Mateo Murphy

unread,
Sep 13, 2009, 5:12:38 PM9/13/09
to rubyonra...@googlegroups.com
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!

rouffj

unread,
Sep 14, 2009, 6:35:55 AM9/14/09
to Ruby on Rails: Core
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 : https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/3187

Thanks for your feedbacks.
> >> patch :https://rails.lighthouseapp.com/projects/8994/tickets/3187/a/266544/e...

Jeremy Kemper

unread,
Sep 14, 2009, 7:33:13 AM9/14/09
to rubyonra...@googlegroups.com
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

rouffj

unread,
Sep 14, 2009, 7:42:38 AM9/14/09
to Ruby on Rails: Core
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.
Reply all
Reply to author
Forward
0 new messages