19

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...
Community
  • 1
  • 1
Ziyan Junaideen
  • 3,270
  • 7
  • 46
  • 71

1 Answers1

22

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

ndnenkov
  • 35,425
  • 9
  • 72
  • 104
Heartcroft
  • 1,672
  • 2
  • 19
  • 31
  • I am not sure what a decorator is, neither did I find any Slim Decorator in the web, could you give me a hint. I just am curious to know. I will try it and if it works accept as an answer. Thanks for the great help! – Ziyan Junaideen May 04 '13 at 17:24
  • Great it worked! But would you be kind enough to tell me what `decorator` is? – Ziyan Junaideen May 04 '13 at 17:29
  • 2
    A decorator is just another way to extract logic from the views to keep them clean, if you want to get started with them I suggest you take a look at the draper gem https://github.com/drapergem/draper. There is a railcast somewhere to learn the basics as well. :) – Heartcroft May 04 '13 at 19:07