-1

Im trying to declare a coffescript function and call it later in the code. I'm pretty new to this, and can't seem to find anything on it.

Here is the code im working on, as you can see I try to call the square function:

coffee:
  @square = (x) -> x * x

p square(3)

Here i want to get a p-tag containing "9"

danopz
  • 3,310
  • 5
  • 31
  • 42
Idva
  • 184
  • 2
  • 12
  • it is not possible to execute coffescript in .erb files. If you want a view helper, just make a rails helper – Yury Lebedev Jun 23 '15 at 13:40
  • How would you print a javascript variable in erb / slim in the first place? – fylooi Jun 23 '15 at 13:52
  • it's not a .erb file, its a .html.slim file – Idva Jun 23 '15 at 13:53
  • that makes no difference if it is a .erb, or .haml or .slim template. The rails template parser cannot execute coffeescript, it can only convert coffeescript to javascript, and output it in the html – Yury Lebedev Jun 23 '15 at 14:16
  • Ok, thank you. I've obviously been attacking this the wrong way. Thank you for answering – Idva Jun 23 '15 at 14:53

1 Answers1

3

You can do it like so

p
 coffee:
   document.write @square(3) 

But !!NEVER EVER!! write such horrible things. In ROR Slim templates are for ruby only. And please don't define any methods there.

MightyKho
  • 46
  • 3