javascript 如何在 Rails 4 中将 javascript_include_tag 与 js.erb 文件一起使用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32211252/
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
How do I use javascript_include_tag with js.erb file in Rails 4?
提问by Abram
Here is what I have in my view:
以下是我的看法:
<%= javascript_include_tag "social_sharing.erb" %>
Then I have: assets/javascripts/social_sharing.erb.js
which includes some ERB tags like <%= post.id %>
...
然后我有:assets/javascripts/social_sharing.erb.js
其中包括一些 ERB 标签,例如<%= post.id %>
...
The first problem is I see this error message:
第一个问题是我看到此错误消息:
`Asset filtered out and will not be served: add `Rails.application.config.assets.precompile += %w( social_sharing.erb.js )` to `config/initializers/assets.rb` and restart your server
If I follow those directions, I then get the js file served, but none of the ERB tags have been evaluated, so I still see <%= post.id %>
in the linked js file.
如果我按照这些说明进行操作,我就会得到 js 文件,但是没有评估任何 ERB 标签,所以我仍然<%= post.id %>
在链接的 js 文件中看到。
回答by trvrfrd
The file should be named assets/javascripts/social_sharing.js.erb
(with .erb on the end), otherwise the Rails asset pipeline won't know to process it and will serve it up as if it were plain JavaScript. The include tag should read <%= javascript_include_tag "social_sharing.js" %>
rather than .erb
, although the .js is optional.
该文件应该被命名assets/javascripts/social_sharing.js.erb
(以 .erb 结尾),否则 Rails 资产管道将不知道如何处理它,并将其作为普通 JavaScript 提供。尽管.js 是可选的,但include 标记应该读取<%= javascript_include_tag "social_sharing.js" %>
而不是。.erb
Everything in the assets folder will get processed through the asset pipelineand can be easily pulled in with asset helper tags.
资产文件夹中的所有内容都将通过资产管道进行处理,并且可以使用资产助手标签轻松拉入。
HOWEVER, javascript_include_tag
and the asset pipeline are for truly static assets that will only be precompiled once and then the same file served with every relevant request. If you're trying to include dynamic content such as <%= post.id %>
that will vary from request to request, you will probably want to move the .js.erb file to somewhere in the views directory and render it as a partial, similar to the first answer for this question:
然而,javascript_include_tag
资产管道用于真正的静态资产,这些资产只会预编译一次,然后为每个相关请求提供相同的文件。如果您尝试包含动态内容,例如<%= post.id %>
会因请求而异,您可能希望将 .js.erb 文件移动到视图目录中的某个位置并将其呈现为部分内容,类似于第一个答案这个问题:
<script>
render partial: '[somewhere]/social_sharing', post: @post
</script>
The file will need to be renamed _social_sharing.js.erb
as well.
该文件也需要重命名_social_sharing.js.erb
。
回答by Elvn
I'm not sure this is the answer, but what you're doing with your asset pipeline vis a vis the javascript looks unusual.
我不确定这就是答案,但是与 javascript 相比,您对资产管道所做的事情看起来很不寻常。
To include the js in your assets precompile add this line in your /config/initalizers/assets.rb file.
要将 js 包含在您的资产预编译中,请在您的 /config/initalizers/assets.rb 文件中添加这一行。
Rails.application.config.assets.precompile += %w( social_sharing.js )
I think once you include this file in your asset pipeline compilation you do not need to use the javascript_include_tag
at all.
我认为一旦您将这个文件包含在您的资产管道编译中,您就根本不需要使用javascript_include_tag
。
If you try this, please let me if you still have the error and or the rendering problem.
如果你尝试这个,如果你仍然有错误和/或渲染问题,请告诉我。