Created
August 13, 2013 19:34
-
-
Save paulingham/6224821 to your computer and use it in GitHub Desktop.
Simple cleaner view for the announcement gem.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#### 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