I've got a smallish patch at http://rails.lighthouseapp.com/projects/8994/tickets/791-add-empty_template-option-when-rendering-a-collection
that i'd be interested in feedback on
in a nutshell
<% if results.any? %>
<%= render :partial => 'result', :collection => results %>
<% else %>
<em> Nothing found </em>
<% end %>
BAD
<%= render :partial => 'result', :collection => results, :empty =>
'<em>Nothing Found</em>' %>
GOOD
Fred
<% render :partial => 'result', :collection => 'results',
:empty_template => 'shared/no_results' %>
or some such.
Locals passed explicitly to the collection partial should probably
also be provided to the empty_template partial.
+1 on an empty result-set option
-1 on default _empty_result partial
I think render should return nil, so that you can do something even
clearer to the eye:
<%= render(:partial => @results) || "Nothing found." %>
(This is backwards compatible too, as ERb will call #to_s on nil).
Yeah, I don't like the idea either, adding a bunch of options to
render to cater for this just seems counter intuitive. Typically it's
not this simple either, if @items is empty I probably don't want to
render the <table> or </table> elements which the _item partial
assumes is there.
--
Cheers
Koz
Yeah, I'm sure it's because of Safari not reading your response if you
don't give it at least a space character.
Returning nil is a tiny breakage (it only breaks the case where you
blindly trust a method to return a String and call + on that. You
should be calling to_s on what you get anyways.)
I'm -1 on adding the conditional logic to render. I like this even better:
<%= @posts.blank? ? render(:partials => @posts) : "Nothing found." %>
or
<%= render(:partial => @posts.blank? ? @posts : 'empty_partial') %>
Rendering nil should probably be the same as rendering an empty
collection. I'm currently chasing a bug where render :partial
with :collection => nil blows up, though I'm having trouble
reproducing it reliably. Sounds like this change might sidestep that
bug though.
--
Josh Susser
http://blog.hasmanythrough.com