Topic: Problem when rendering a partial if :collections is nil (empty) !

Hi all,

I'm looking for a nice way of "not rendering" a partial if its collections is nil.
Let's take an example :

I have an itune like ajax application (with playlists name on the left and playlist details on the right).
To render the list of playlist, I use a partial (call it playlist_list) that consume a collection of playlists.
So, we have :

render(:partial => "playlist_list", :collection => @playlists).

If I have some playlist in my database (at least one) then everything works fine but, if @playlists is
empty, then it raises an error ...

-> What could be the best way to avoid this ?
-> use an if statement in the main view to see if @playlists is nil (if nil then I do not render otherwise I do render the partial)
(I don't think that the best solution ... logic in the view ... hum not very nice but is there another solution ???, like :conditions in the partial render ?)

Thanks for your help !!!

Julien

Re: Problem when rendering a partial if :collections is nil (empty) !

<% if !@playlists.nil? #if not playlists == null

render(:partial => "playlist_list", :collection => @playlists)

end %>

should work

Re: Problem when rendering a partial if :collections is nil (empty) !

Hi evamedia

Thanks for your answer ;-)
Unfortunatly this doesn't work.
May be 'cos @playlists is not nil but simply empty.

When I thought that I replace nil with empty ( as @playlists is supposed to be an array - I presume ...).
This doesn't work either !

so : !@playlists.nil? or !@playlists.empty? does not work ...

Now i have no idea how to test this "meptyness" :-( sad ...

Jules

Re: Problem when rendering a partial if :collections is nil (empty) !

You might find this is pretty easy and very reliable:

render(:partial => "playlist_list", :collection => @playlists) unless @playlists.nil? || @playlists.empty?

Re: Problem when rendering a partial if :collections is nil (empty) !

me again ... found it ... stupid mistake ... as usual ...
(I don't say that for you ;-))

In fact if I write the if statement with only <% instead of <%= the render is never "render" ...
so the staement should be :

<% if !@playlists.empty? %>
<%= render(:partial => "playlist_list", :collection => @playlists) %>
<% end %>

and it works ;-)

Might be nice to do a helper for that ...
I'll check and come back with a method.

Anyway, that works but it would be great to have a :conditions statement in the render that will only render if :collection is not empty ... may be in rails 1.2

Jules

Re: Problem when rendering a partial if :collections is nil (empty) !

hi danger !!

I like that !!! that's the ruby / rails way ;-)
Smart, elegant, one line ... just what I was looking for !!!

I wish I could find it myself ... this is a proof of good ruby/rails coder ;-)

Thanks a lot !

Re: Problem when rendering a partial if :collections is nil (empty) !

BTW, instead of this:

@playlists.nil? || @playlists.empty?

You can do:

@playlists.blank?

Railscasts - Free Ruby on Rails Screencasts