Skip to content

Instantly share code, notes, and snippets.

@paulingham
Created August 13, 2013 19:34
Show Gist options
  • Save paulingham/6224821 to your computer and use it in GitHub Desktop.
Save paulingham/6224821 to your computer and use it in GitHub Desktop.
Simple cleaner view for the announcement gem.
#### Your view (blah.html.erb) ####
<%= render partial: 'announcement', collection: Article.newest %>
#### _announcement.html.erb ####
<%= announce announcement, { format: 'bootstrap' } %>
#### Explanation ####
If Article.newest returns an array of announcements (one or more) it will automatically render the partial 'announcement' a number of times. So if Article.newest returns [<Article @id=1>, <Article @id=2>], the announcement.html.erb file will be rendered twice. If Article.newest returns [], then it'll render nothing.
You'll obviously need to check for typos. But it removes the need for you to do
<% if Article.newest.present? %>
<% end %> and removes all conditions logic from your views.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment