Maxxx
Maxxx

Reputation: 1179

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 !

Upvotes: 30

Views: 18997

Answers (6)

Nate
Nate

Reputation: 13252

This helped with whitespace for me.

<i class="<%
  case blog_post_type
  when :pencil %>fa fa-pencil<%
  when :picture %>fa fa-picture-o<%
  when :film %>fa fa-film<%
  when :headphones %>fa fa-headphones<%
  when :quote %>fa fa-quote-right<%
  when :chain %>fa fa-chain<%
  end
%>"></i>

Upvotes: 3

Guillaume
Guillaume

Reputation: 705

You can also use <%- case index -%> syntax:

<% @prods.each_with_index do |prod, index| %>
  <%- case index -%>
  <%- when 0 -%><%= image_tag prod.img, :id => "one") %>
  <%# ... %>
  <%- end -%>
<% end %>

Upvotes: 3

fl00r
fl00r

Reputation: 83680

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

Upvotes: 63

beanie
beanie

Reputation: 1448

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

Upvotes: 20

D_Bye
D_Bye

Reputation: 909

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.

Upvotes: 4

JohnColvin
JohnColvin

Reputation: 2509

I think in ERB, you'll have to put the conditions on the line under the whens. Like this:

<% @prods.each_with_index do |prod, index| %>
  <% case index %>
    <% when 0 %>
      <%= image_tag("#{prod}", :id => "one") %>
    <% when 1 %>
      <%=  image_tag("#{prod}", :id => "two") %>
    <% when 2 %>
      <%= image_tag("#{prod}", :id => "three") %>
  <% end %>
<% end %>

Ruby supports case-whens with one line conditions with the then keyword, but I don't think ERB can parse them correctly. Ex:

case index
    when 0 then "it's 0"
    when 1 then "it's 1"
    when 2 then "it's 2"
end

Upvotes: 2

Related Questions