我的控制器中有以下变量:
class MyController < ApplicationController
def my_method
@status = "status"
end
end
在我的haml
视图中,我尝试了跟随,但它不起作用(因为它使用默认的.erb语法):
#app/views/mycontroller/me_method.html.haml
:javascript
alert(<%=raw @status %>)
如何在内联JavaScript中使用@status
变量?
您可以使用简单的“#{}”插值标记将ruby变量与HAML一起使用。
你的代码:
:javascript
alert(<%=raw @status %>)
可能解决方案
:javascript
alert("#{@status}")
使用普通字符串插值语法(#{}
)。
#app/views/mycontroller/me_method.html.haml
:javascript
alert(#{raw @status})
另见此前的SO问题:
就像你在ruby字符串中所做的那样:
:javascript
alert("#{raw @status}")