I have a Rails controller in which I am setting a instance variable -
我有一个Rails控制器,我在其中设置一个实例变量-
@user_name = "Some Username"
In my .slim template I am using coffee engine to generate javascript and want to print out the user name from client-sie javascript code -
在我的。slim模板中,我使用咖啡引擎生成javascript,并想从客户端javascript代码中打印出用户名——
coffee:
$(document).ready ->
name = "#{@user_name}"
alert name
But this is the javascript that is being generated??
但这就是正在生成的javascript ?
$(document).ready(function() {
var name;
name = "" + this.my_name;
alert(name);
}
How do I access controller instance variables in my CoffeeScript code??
如何访问CoffeeScript代码中的控制器实例变量?
I am tagging this as haml since I am guessing haml will have the same issue when using CoffeeScript .
我把这个标记为haml,因为我猜haml在使用CoffeeScript时也会有同样的问题。
3 个解决方案
#1
87
What's happening is that "#{@user_name}"
is being interpreted as CoffeeScript, not as Ruby code that's evaluated and injected into the CoffeeScript source. You're asking, "How do I inject a Ruby variable into my CoffeeScript source?"
所发生的是“#{@user_name}”被解释为CoffeeScript,而不是被评估并注入CoffeeScript源代码的Ruby代码。您会问,“我如何将Ruby变量插入到CoffeeScript源文件中?”
The short answer is: Don't do this. The Rails team made an intentional decision not to support embedded CoffeeScript in templates in 3.1, because there's significant performance overhead to having to compile CoffeeScript on every request (as you'd have to do if you allowed arbitrary strings to be injected into the source).
简短的回答是:不要这样做。Rails团队故意决定不支持3.1中模板中的嵌入式CoffeeScript,因为在每个请求上都必须编译CoffeeScript会带来很大的性能开销(如果您允许任意字符串被注入到源中,那么就必须这样做)。
My advice is to serve your Ruby variables separately as pure JavaScript, and then reference those variables from your CoffeeScript, e.g.:
我的建议是将Ruby变量作为纯JavaScript单独提供,然后从CoffeeScript引用这些变量,例如:
javascript:
user_name = "#{@user_name}";
coffee:
$(document).ready ->
name = user_name
alert name
#2
32
I tend to avoid inline javascript at all costs.
我倾向于不惜一切代价避免内联javascript。
A nice way to store variables in your HTML, to be used from your javascript, is to use the HTML5 data-attributes. This is ideal to keep your javascript unobtrusive.
在HTML中存储变量(从javascript中使用)的一个好方法是使用HTML5数据属性。这是保持javascript不突兀的理想方法。
#3
2
You could also use:
你也可以使用:
$(document).ready ->
name = <%= JSON.generate @user_name %>
alert name
This is because JSON is a subset of JavaScript.
这是因为JSON是JavaScript的一个子集。
#1
87
What's happening is that "#{@user_name}"
is being interpreted as CoffeeScript, not as Ruby code that's evaluated and injected into the CoffeeScript source. You're asking, "How do I inject a Ruby variable into my CoffeeScript source?"
所发生的是“#{@user_name}”被解释为CoffeeScript,而不是被评估并注入CoffeeScript源代码的Ruby代码。您会问,“我如何将Ruby变量插入到CoffeeScript源文件中?”
The short answer is: Don't do this. The Rails team made an intentional decision not to support embedded CoffeeScript in templates in 3.1, because there's significant performance overhead to having to compile CoffeeScript on every request (as you'd have to do if you allowed arbitrary strings to be injected into the source).
简短的回答是:不要这样做。Rails团队故意决定不支持3.1中模板中的嵌入式CoffeeScript,因为在每个请求上都必须编译CoffeeScript会带来很大的性能开销(如果您允许任意字符串被注入到源中,那么就必须这样做)。
My advice is to serve your Ruby variables separately as pure JavaScript, and then reference those variables from your CoffeeScript, e.g.:
我的建议是将Ruby变量作为纯JavaScript单独提供,然后从CoffeeScript引用这些变量,例如:
javascript:
user_name = "#{@user_name}";
coffee:
$(document).ready ->
name = user_name
alert name
#2
32
I tend to avoid inline javascript at all costs.
我倾向于不惜一切代价避免内联javascript。
A nice way to store variables in your HTML, to be used from your javascript, is to use the HTML5 data-attributes. This is ideal to keep your javascript unobtrusive.
在HTML中存储变量(从javascript中使用)的一个好方法是使用HTML5数据属性。这是保持javascript不突兀的理想方法。
#3
2
You could also use:
你也可以使用:
$(document).ready ->
name = <%= JSON.generate @user_name %>
alert name
This is because JSON is a subset of JavaScript.
这是因为JSON是JavaScript的一个子集。