Reputation: 1033
I have a problem with my each loop. The line that prints programdetail.name and programdetail.bodypart doesn't print the value. Also do you know how can I make this loop a little bit more efficient? I want to print first 2 items with class "odd" and the other 2 with non-class. So and so forth.
<% @counter = 0 %>
<% @program.programdetails.each do |programdetail| %>
<% @counter = @counter + 1 %>
<% @counter = @counter % 3 %>
<% if (@counter == 0)
@counter -= 1
end %>
<%= '<h3 class="odd"><span class="moduleLabel"> #{programdetail.name}</span><span class="moduleDescription">#{programdetail.bodypart}</span></h3>' if @counter != 0 %>
<%= '<h3><span class="moduleLabel">#{programdetail.name}</span><span class="moduleDescription">#{programdetail.bodypart}</span></h3>' if @counter != 0 %>
<% end %>
Upvotes: 0
Views: 1910
Reputation: 13014
cycle
helper could have worked, if you wanted odd/even combo, or over a collection:
<% @program.programdetails.each do |programdetail| %>
<h3 class="<%= cycle("odd", "odd", "", "") %>
<span class="moduleLabel"><%= programdetail.name %></span>
<span class="moduleDescription"><%= programdetail.bodypart %></span>
</h3>
<% end %>
To fix your code:
<% @counter = 0 %>
<% @program.programdetails.each do |programdetail| %>
<% @counter = (@counter % 4) + 1 %>
<h3 class="<%= ((1..2).cover?(@counter))? 'odd': '' %>">
<span class="moduleLabel"><%= programdetail.name %></span>
<span class="moduleDescription"><%= programdetail.bodypart %></span>
</h3>
<% end %>
Upvotes: 3