5

I have the following variable in my controller:

class MyController < ApplicationController

  def my_method
    @status = "status"
  end
end

In my haml view, I tried following, but it doesn't work (since it's using the default .erb syntax):

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

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

How can I use the @status variable inside my inline JavaScript?

halfer
  • 19,824
  • 17
  • 99
  • 186
sameera207
  • 16,547
  • 19
  • 87
  • 152

3 Answers3

5

You can use the simple "#{}" interpolation tags to use ruby variables with HAML.

Your code:

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

Possible Solution:

:javascript
    alert("#{@status}")
Vageesh Bhasin
  • 553
  • 2
  • 12
3

Use the normal 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?

Community
  • 1
  • 1
user513951
  • 12,445
  • 7
  • 65
  • 82
2

Just like you would do in a ruby string :

:javascript
  alert("#{raw @status}")
Intrepidd
  • 19,772
  • 6
  • 55
  • 63