Is there a way to write a case statement in Slim for the following example. I made some changes for the StackOverflow question: How to write a switch statement in Ruby
case a
when 1..5
= "It's between 1 and 5"
when 6
= "It's 6"
when String
= "You passed a string"
else
= "You gave me #{a} -- I have no idea what to do with that."
end
I tried the following but generates an error.
= case @taxon.name
= when "Wedding Cakes"
div.taxon-descripiton Wedding Cake Description
This is what I am told
... syntax error, unexpected tIVAR, expecting keyword_when
; @output_buffer.safe_concat(("<when>\"Weddin...
You can't use “||” in case names.
The case statement is a multiway branch statement just like a switch statement in other languages. It provides an easy way to forward execution to different parts of code based on the value of the expression.
Ruby uses the case for writing switch statements. As per the case documentation: Case statements consist of an optional condition, which is in the position of an argument to case , and zero or more when clauses.
Slim is a page-templating language that minimizes markup and syntax. It removes most of the extra "programming-like" symbols from HTML so that your code looks cleaner. Slim also adds if-else statements, loops, includes, and more.
=
is used when you want to render something, when you want to call some ruby logic without rendering anything in a view you should use -
:
- case @taxon.name
- when "Wedding Cakes"
div.taxon-descripiton Wedding Cake Description
I am not sure this will solve your problem though, have you considered moving this logic to a decorator anyway?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With