2014-02-17 32 views
3

我已经在我的控制器以下变量怎样才能在HAML

class MyController < ApplicationController 

    def my_method 
    @status = "status" 
    end 
end 
haml观点

,我尝试以下使用红宝石变量内嵌的JavaScript,但它不工作(因为其使用默认.erb语法)

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

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

我如何使用我的在线Java脚本里面的@status变量,提前

+0

除非'me_method'我找不到任何错误的可能错字'my_method'。 –

回答

3

您可以使用简单的 “#{}”插值标签在HAML中使用ruby变量。

您的代码:

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

可能的解决方案:

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

正常使用的串interpolati感谢在语法上(#{})。

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

:javascript 
    alert(#{raw @status}) 

又见这个以前的SO问题:

Inline ruby in :javascript haml tag?

2

就像你会在Ruby字符串做:

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