简体   繁体   English

在Javascript / jQuery中访问Rails全局常量

[英]Access Rails global constant in Javascript / jQuery

My setup: Rails 3.0.9, Ruby 1.9.2, jQuery 1.6.2 我的设置:Rails 3.0.9,Ruby 1.9.2,jQuery 1.6.2

Rails 滑轨

constants.rb (initializer file)

DEFAULT_REPLY = "Add a reply..."

Rails 滑轨

index.html.erb 

<%= javascript_include_tag 'reply' %>
...(rest of view code)...

reply.js

$(function() {
 var default_reply = <%= h DEFAULT_REPLY -%>;
 ...(rest of jQuery code)...
});

This throws an error Uncaught SyntaxError:Unexpected token %= , I tried to enclose it in quotes like var default_reply = '<%= h DEFAULT_REPLY -%>' and it output the value as is, meaning default_value has the value of <%= h DEFAULT_REPLY -%>' which is clearly not what I intended. 这将引发错误Uncaught SyntaxError:Unexpected token %= ,我试图将其括在诸如var default_reply = '<%= h DEFAULT_REPLY -%>'引号中,并按原样输出值,这意味着default_value的值为<%= h DEFAULT_REPLY -%>'显然不是我想要的。 What am I doing wrong? 我究竟做错了什么?

EDIT Given the feedback, I have reconsidered and is now using a local variable and jQuery to pull the value from the textarea upon page load. 编辑鉴于反馈,我已经重新考虑,现在使用局部变量和jQuery在页面加载时从textarea中提取值。

You need to add .erb to the end of your .js file, along with add the quotes around the string. 您需要将.erb添加到.js文件的末尾,并在字符串周围添加引号。

The name of your javascript file should be reply.js.erb. 您的javascript文件的名称应为reply.js.erb。

Currently, your .js file is not being run through rails, and is being served statically. 当前,您的.js文件未通过Rails运行,而是以静态方式提供。 That is why when you put the quotes around the string, it output the string '<%= h DEFAULT_REPLY %>' instead of the correct text. 这就是为什么将引号括在字符串中时会输出字符串“ <%= h DEFAULT_REPLY%>”而不是正确的文本的原因。

I'd strongly recommend against this approach but I think you're confusing two things here. 我强烈建议您不要使用这种方法,但是我认为您在这里混淆了两件事。

Assuming reply.js is in public/javascripts/reply.js , this is a static JS file that is served up by your server. 假设reply.js位于public/javascripts/reply.js ,这是服务器提供的静态JS文件。 You cannot put any dynamic ("server side") code in here as the file is not evaluated in any manner, just passed back statically. 您不能在此处放置任何动态(“服务器端”)代码,因为文件不会以任何方式进行评估,而只是静态返回。

If you want a global JS variable to use in your files, you'd need to do assign it in your layout file app/views/layouts/application.html.erb or in your action files ( index.html.erb , show.html.erb , etc). 如果要在文件中使用全局JS变量,则需要在layout文件app/views/layouts/application.html.erb或操作文件( index.html.erbshow.html.erb进行分配show.html.erb等)。

Any ERB file is evaluated before returning it, so you could put 在返回任何ERB文件之前都会对其进行评估,因此您可以将

<script type='text/javascript'>
  $(function() {
   var default_reply = "<%= escape_javascript DEFAULT_REPLY %>";
  });
</script>

above the <%= yield %> statement of your layout file. 高于layout文件的<%= yield %>语句。

Again, I'd STRONGLY recommend against this approach but if that's why you need, I think this will solve it. 再次,我强烈建议您不要使用这种方法,但是如果您要这么做,我认为这可以解决。

It sounds like you named your view template incorrectly, does it end in .html.erb? 听起来您的视图模板命名不正确,它以.html.erb结尾吗? If not, it won't evaluate the ERB fragment you pasted. 如果不是,它将不会评估您粘贴的ERB片段。

Once you fix that, you can embed what you want with the following ERB code: 解决此问题后,您可以使用以下ERB代码嵌入所需内容:

$(function() {
 var default_reply = "<%= h DEFAULT_REPLY -%>";
 ...
});

If it is a rails 3 App why are you using that syntax? 如果它是Rails 3 App,为什么要使用该语法? try: 尝试:

var default_reply = <%= DEFAULT_REPLY %>;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM