[英]Injecting variable values into javascript and HAML in RoR
I have the following function for using ZenDesk. 我有以下使用ZenDesk的功能。 I'd like to inject my current_user details into the form as follows. 我想将current_user详细信息注入表单,如下所示。 (this is my from html.haml template). (这是我的html.haml模板)。 However I cannot figure out how to make this work. 但是,我无法弄清楚如何使这项工作。
: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变量注入haml中的:javascript元素。
This should work ie. 这应该工作,即。 put all inline ruby inside of #{}
: 将所有内联红宝石放在#{}
:
requester_name: "#{current_user.first_name + ' ' + current_user.last_name if current_user}",
requester_email: "#{current_user.email if current_user}",
Direct #{}
works for simple strings, but is not the most scalable / safe solution in general. Direct #{}
适用于简单字符串,但通常不是最具扩展性/安全性的解决方案。
For example, the literal backslash in Ruby would cause you problems in Javascript where it will be interpreted as a newline character: 例如,Ruby中的字面反斜杠会导致您在Javascript中出现问题,它将被解释为换行符:
- a = "\\n"
:javascript
'#{ a }' !== "\\n"
From this awesome Rails cast , the following techniques can be used: 从这个令人敬畏的Rails演员 ,可以使用以下技术:
escape_javascript escape_javascript
Alias: j
. 别名: j
。
Works only on strings. 仅适用于字符串。
Escapes characters that can have special meanings in Javascript strings, like backslash escapes, into a format suitable to put inside Javascript string literal quotes. 将Javascript字符串中具有特殊含义的字符(如反斜杠转义)转换为适合放入Javascript字符串文字引号的格式。
Maintain html_safe
status of input, so needs html_safe
otherwise special HTML chars like <
would get escaped into <
保持输入的html_safe
状态,因此需要html_safe
否则特殊的HTML字符,如<
将被转义为<
. 。
- a = "\\n<"
:javascript
'#{ j(a) }' === '\\n<'
'#{ j(a).html_safe }' === '\\n<'
to_json + html_safe to_json + html_safe
Works because JSON is almost a subset of Javascript object literal notation . 可以工作,因为JSON 几乎是Javascript对象文字表示法的一个子集 。
Works on any hash object, including strings, arrays and integers which are converted to JSON fragments of the corresponding data type. 适用于任何哈希对象,包括转换为相应数据类型的JSON片段的字符串,数组和整数。
- data = { key1: 'val1', key2: 'val2' }
:javascript
data = #{ data.to_json }
data.key1 === 'val1'
data.key2 === 'val2'
data- attributes 数据属性
Add values to attributes, retrieve them with Javascript DOM operations. 向属性添加值,使用Javascript DOM操作检索它们。
Better with the content_tag
helper: 使用content_tag
帮助器更好:
= content_tag 'div', '', id: 'data', data: {key1: 'val1', key2: 'val2'}
:javascript
$('#data').data('key1') === 'val1'
$('#data').data('key2') === 'val2'
gon 坤
Library specialized for the job: https://github.com/gazay/gon 专门为这项工作的图书馆: https : //github.com/gazay/gon
Probably the most robust solution. 可能是最强大的解决方案。
Gemfile: 的Gemfile:
gem 'gon'
Controller: 控制器:
gon.key1 = 'val1'
gon.key2 = 'val2'
Layout app/views/layouts/application.html.erb
: 布局app/views/layouts/application.html.erb
:
<html>
<head>
<meta charset="utf-8"/>
<%= include_gon %>
View: 视图:
:javascript
gon.key1 === 'val1'
gon.key2 === 'val2'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.