2014-02-17 31 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}")