如何在Haml中的内联JavaScript中使用Ruby变量?

5

我在控制器中有以下变量:

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变量?

除了可能的拼写错误 my_method 改为 me_method,我找不到任何问题。 - Billy Chan
3个回答

5
你可以使用简单的“#{}”插值标签,将Ruby变量与HAML一起使用。
你的代码:
:javascript
    alert(<%=raw @status %>)

可能的解决方案:
:javascript
    alert("#{@status}")

3

2

就像你在 Ruby 字符串中所做的一样:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接