为数据属性插入ruby变量?

Gun*_*ter 2 ruby variables haml attributes

在.html.haml文件中,我有一个按钮:

%button{:data=>{'foo'=> @bar}, type:'button', 'id'=>'confirmBtn'} Confirm
Run Code Online (Sandbox Code Playgroud)

但是,data属性没有任何东西.在这种情况下插入ruby变量的正确方法是什么?

Kev*_*vin 5

这应该这样做:

%button{ data: { foo: @bar.to_s }, type: 'button', id: 'confirmBtn' } Confirm
Run Code Online (Sandbox Code Playgroud)

我已经改变了上面一行中的语法,但你应该做的是@bar改为@bar.to_s(正如tadman建议的那样).它看起来比"#{@bar}"我之前建议的更清洁.