Say I have a block like this:
<% @help_sections.each do |section| %>
<li><%= section.name %></li>
<% end %>
But on the last record returned, I want to do something else, e.g. applying a class to the li that’s there:
<li class="last"><%= section.name %></li>
How do I do that in the most DRY way?
Thanks.
Edit1:
I imagine I would simply use an if statement and the last ruby method, but not sure how to do that within the block? I know that if I just wanted the last element in that array, I could just do @help_sections.last, but that doesn’t make sense within the confines of a Ruby block.
The most DRY way is to use CSS instead. Instead of e.g. this:
..and then cluttering up your markup with an extra CSS class, just use the
:last-childpseudoselector, i.e.:Then you don’t have to change anything in your view. This is supported in all modern browsers including IE9.