Skip to content

Instantly share code, notes, and snippets.

@franciscoj
Created July 17, 2010 13:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save franciscoj/479514 to your computer and use it in GitHub Desktop.
Save franciscoj/479514 to your computer and use it in GitHub Desktop.
how to handle render :collection => @foos in rails when @foos is an empty collection
<!-- app/views/foos/_foo.html.erb -->
<tr>
<td>
<%= foo.name %>
</td>
<td>
<%= foo.color %>
</td>
</tr>
<!-- app/views/foos/_foos.html.erb -->
<table>
<thead>
<tr>
<th>Name</th>
<th>Color</th>
</tr>
</thead>
<tbody>
<%= render @foos %>
</tbody>
</table>
<!-- app/views/foos/_foos_empty.html.erb -->
<p>No foos yet... <%= link_to "create some!", new_foo_path %></p>
# app/helpers/foos_helper.rb
module FoosHelper
def render_foos
if @foos.blank?
render :partial => "foos_empty"
else
render :partial => "foos"
end
end
end
<!-- app/views/foos/index.html.erb -->
<%= render_foos %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment