Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Case statement in Slim

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...
like image 680
Ziyan Junaideen Avatar asked May 04 '13 14:05

Ziyan Junaideen


People also ask

Can you use || IN CASE statement Ruby?

You can't use “||” in case names.

What is a case statement in Ruby?

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.

Does Ruby have a switch statement?

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.

What are .slim files?

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.


1 Answers

= 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?

like image 73
Genís Avatar answered Sep 20 '22 13:09

Genís