将变量值注入 RoR 中的 javascript 和 HAML
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4708369/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Injecting variable values into javascript and HAML in RoR
提问by Dave G
I have the following function for using ZenDesk. I'd like to inject my current_user details into the form as follows. (this is my from html.haml template). However I cannot figure out how to make this work.
我有以下使用 ZenDesk 的功能。我想将我的 current_user 详细信息注入表单,如下所示。(这是我来自 html.haml 模板)。但是,我无法弄清楚如何进行这项工作。
:javascript
if (typeof(Zenbox) !== "undefined") {
Zenbox.init({
dropboxID: "xxxxx",
url: "xxxxx.zendesk.com",
tabID: "support",
tabColor: "black",
tabPosition: "Left",
requester_name: =current_user ? "#{current_user.first_name} #{current_user.last_name}" : "" ,
requester_email: =current_user ? "#{current_user.email}" : "" ,
hide_tab: true
});
}
In short, how does one inject rails variables into a :javascript element in haml.
简而言之,如何将 rails 变量注入到 :javascript 元素中。
回答by Heikki
This should work ie. put all inline ruby inside of #{}
:
这应该工作,即。将所有内联 ruby 放在#{}
:
requester_name: "#{current_user.first_name + ' ' + current_user.last_name if current_user}",
requester_email: "#{current_user.email if current_user}",