Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use a Ruby variable in an inline JavaScript in haml?

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?

like image 418
sameera207 Avatar asked Feb 17 '14 09:02

sameera207


1 Answers

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

Your code:

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

Possible Solution:

:javascript
    alert("#{@status}")
like image 135
Vageesh Bhasin Avatar answered Oct 03 '22 13:10

Vageesh Bhasin