Rails switch case in the view
You should pull your first when
into same block as case
<% @prods.each_with_index do |prod, index|%> <% case index when 0 %><%= image_tag prod.img, :id => "one") %> <% when 1 %><%= image_tag prod.img, :id => "two") %> <% when 2 %><%= image_tag prod.img, :id => "three") %> <% end %><% end %>
Don't put to much logic in your views.
i would add a helper
def humanize_number(number) humanized_numbers = {"0" => "zero", "1" => "one"} humanized_numbers[number.to_s]end
than you can call it from the view with
<%= image_tag("#{prod.img}", :id => humanized_number(index)) %>
First, you should really think about abstracting this functionality into a helper method to avoid cluttering your views with logic.
Second, case statements are a bit tricky to use in ERB, because of the way erb parses the code. Try instead (not tested, as I don't have a ruby close to hand at the moment):
<% @prods.each_with_index do |prod, index|%> <% case index when 0 %> <%= image_tag("#{prod.img}", :id => "one") %> <% when 1 %> <%= image_tag("#{prod.img}", :id => "two") %> <% when 2 %> <%= image_tag("#{prod.img}", :id => "three") %> <% end %><% end %>
See this thread for more information.