How can I use ruby ​​variable in inline javascript in haml

I have the following variable in my controller

class MyController < ApplicationController

  def my_method
    @status = "status"
  end
end

in my view haml, I tried following, but it does not work (since it uses the default .erb syntax)

#app/views/mycontroller/me_method.html.haml

:javascript
  alert(<%=raw @status %>)

How can I use a variable @statusinside my built-in java script, thanks in advance

+3
source share
3 answers

You can use simple "# {}" interpolation tags to use ruby ​​variables using HAML.

Your code:

:javascript
    alert(<%=raw @status %>)

Possible Solution:

:javascript
    alert("#{@status}")
+3
source

Use the standard string interpolation syntax ( #{}).

#app/views/mycontroller/me_method.html.haml

:javascript
  alert(#{raw @status})

See also this previous SO question:

Inline ruby ​​in: javascript haml tag?

+2

:

:javascript
  alert("#{raw @status}")
+2

All Articles