Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails switch case in the view

I want to write a switch case in my view :

<% @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 %> 

But it doesn't work. Do I have to add a <% end %> somewhere in each line ? Any ideas ? Thanks !

like image 758
Maxxx Avatar asked Mar 07 '12 14:03

Maxxx


1 Answers

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 %> 
like image 138
fl00r Avatar answered Oct 03 '22 23:10

fl00r