I want to write a switch case in my view :
"one") %> "two") %> "three") %>
But it doesn't work. Do I have to add a somewhere in each line ? Any ideas ? Thanks !
I want to write a switch case in my view :
"one") %> "two") %> "three") %>
But it doesn't work. Do I have to add a somewhere in each line ? Any ideas ? Thanks !
You should pull your first when
into same block as case
"one") %> "two") %> "three") %>
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
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):
"one") %> "two") %> "three") %>
See this thread for more information.
You can also use syntax:
"one") %>
This helped with whitespace for me.
I think in ERB, you'll have to put the conditions on the line under the whens. Like this:
"one") %> "two") %> "three") %>
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