Is there a way to a add a HTML .class to an unordered list item using Ruby or possibly a rails helper method?
Basically I’m just iterating through a todo model
<ul>
<% @todos.each do |todo| %>
<li><%= todo.task %></li>
<% end %>
</ul>
I want to add a class=”third” to the third list item so the HTML output would look like
<ul>
<li>Task 1</li>
<li>Task 2</li>
<li class="third">Task 3</li>
<ul>
I was using jQuery to add the class but I wanted a better way of doing this instead of relaying on javascript.
Thanks all.
One way to do it using each_with_index (the index starts at zero):