Ruby-on-rails javascript_include_tag Rails 4 在生产中生成“/javascripts/”而不是“/assets”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17622858/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:19:14  来源:igfitidea点击:

javascript_include_tag Rails 4 generating "/javascripts/" instead of "/assets" in production

ruby-on-railsasset-pipeline

提问by Stuart

I have a Rails 4 application with

我有一个 Rails 4 应用程序

<%= javascript_include_tag "modernizr", "data-turbolinks-track" => true %>

in the head. In development, the following HTML is rendered, and modernizr is loaded:

在头部。在开发中,渲染了以下 HTML,并加载了 Modernizr:

<script data-turbolinks-track="true" src="/assets/modernizr.js?body=1"></script>

In production, the followign HTML is rendered, and modernizr is notloaded (404 not found):

在生产中,followign HTML 被渲染,并且没有加载Modernizr (404 not found):

<script data-turbolinks-track="true" src="/javascripts/modernizr.js"></script>

In production, /assets/modernizr.jsis found and browsable.

在生产中,/assets/modernizr.js可找到并可浏览。

The Rails documentationsays that the javascript_include_tagshould generate

Rails文档说,javascript_include_tag应该产生

<script data-turbolinks-track="true" src="/assets/modernizr.js?body=1"></script>

In production, my stylesheet_link_tags are fine, linking to the /assets/directory.

在生产中,我的stylesheet_link_tags 很好,链接到/assets/目录。

Why is the javascript_include_taglinking to /javascriptsinstead of /assetsin production, and how can I fix it?

为什么javascript_include_tag链接到/javascripts而不是/assets在生产中,我该如何解决?

采纳答案by mobileAgent

One of the usage statements for AssetUrlHelper indicates it will produce /javascripts/ urls like what you are seeing:

AssetUrlHelper 的使用语句之一表明它会像您看到的那样生成 /javascripts/ url:

# asset_path "application", type: :javascript # => /javascripts/application.js

# asset_path "application",输入::javascript # => /javascripts/application.js

(from asset_url_helper.rb line 117 - [1])

(来自 asset_url_helper.rb 第 117 行 - [ 1])

This code looks like it can only be reached if the precompiled asset is missing so it would appear that your asset compilation is not working (my deployments usually fail when that happens, so maybe yours isn't even firing).

这段代码看起来只有在缺少预编译资产的情况下才能到达,因此看起来您的资产编译不起作用(当发生这种情况时,我的部署通常会失败,因此您的部署可能甚至没有启动)。

The same asset_url_helper.rb calls the /javascripts/ part 'extname' and uses the following map to know how to generate the name:

同样的 asset_url_helper.rb 调用 /javascripts/ 部分 'extname' 并使用以下映射来了解如何生成名称:

 # Maps asset types to public directory.
  ASSET_PUBLIC_DIRECTORIES = {
    audio:      '/audios',
    font:       '/fonts',
    image:      '/images',
    javascript: '/javascripts',
    stylesheet: '/stylesheets',
    video:      '/videos'
  }

A new Rails 4 app has this in the config/environments/production.rb

一个新的 Rails 4 应用程序在 config/environments/production.rb 中有这个

  # Do not fallback to assets pipeline if a precompiled asset is missed.
  config.assets.compile = false

which seems to match the behavior you are seeing.

这似乎与您所看到的行为相匹配。

回答by patkoperwas

By default, Rails only precompiles application.js, application.cssand any images it finds in the assets path. Therefore, in production mordernizr will not get precompiled and thus the javascript helpers will not be able to find the file.

默认情况下,Rails的只有预编译application.jsapplication.css以及任何图片它在资产路径中找到。因此,在生产中 modrnizr 不会被预编译,因此 javascript 助手将无法找到该文件。

In order to fix the issue, you can add modernizr to the precompile list by modifying the following config in production.rb

为了解决这个问题,您可以通过修改以下配置将modernizr添加到预编译列表中 production.rb

config.assets.precompile += ['modernizr.js']

For more information see the Rails Guides

有关更多信息,请参阅Rails 指南

回答by Ross Allen

Be sure to precompile your assets in production by running this command:

请务必通过运行以下命令在生产中预编译您的资产:

RAILS_ENV=production bundle exec rake assets:precompile

The Rails Guide on the asset pipeline can give you more details: http://guides.rubyonrails.org/asset_pipeline.html#precompiling-assets

关于资产管道的 Rails 指南可以为您提供更多详细信息:http: //guides.rubyonrails.org/asset_pipeline.html#precompiling-assets

回答by rbinsztock

I have a new application using Rails 4 deployed on Heroku with :

我有一个使用 Rails 4 部署在 Heroku 上的新应用程序:

<%= javascript_include_tag "application", "data-turbolinks-track" => true %>

my javascript application.(fingerprint).js called from the src: assets/application.js

我的 javascript application.(fingerprint).js 从 src 调用:assets/application.js

i think your problem come from something into your production.rb who define assets from another location.

我认为您的问题来自您的 production.rb 中的某些内容,他们定义了来自另一个位置的资产。

So maybe you can add Moderniz.js to

所以也许你可以将 Moderniz.js 添加到

config.assets.precompile = ['.js', '.css', '*.css.erb']

config.assets.precompile = [' .js','.css','*.css.erb']

in config/production.rb

在 config/production.rb

Or simply require modernizr script into your application.js

或者简单地将 Modernizr 脚本引入到您的 application.js 中

//= require mordernizr

//= 需要现代

and remove the modernizr script call into your layout.

并将 Modernizr 脚本调用删除到您的布局中。

<%= javascript_include_tag "modernizr", "data-turbolinks-track" => true %>

Can you check from where your application.js is served into your production environment ?

您可以检查您的 application.js 是从哪里提供给您的生产环境的吗?

回答by Dylan Karr

It may be because this file needs to be in /vendor/assets/javascript instead of /app/assets/javascript. The Vendor folder is for javascript libraries, and the App folder is for your code.

可能是因为这个文件需要在 /vendor/assets/javascript 而不是 /app/assets/javascript。Vendor 文件夹用于 javascript 库,App 文件夹用于您的代码。

A better solution than adding a tag to your layout would be adding a script reference to your application.js and let the sass compiler compress and attach it to your main javascript file.

比在布局中添加标签更好的解决方案是添加对 application.js 的脚本引用,并让 sass 编译器压缩并将其附加到您的主 javascript 文件中。

If you don't get a definitive answer, check out: http://guides.rubyonrails.org/asset_pipeline.html#asset-organization

如果您没有得到明确的答案,请查看:http: //guides.rubyonrails.org/asset_pipeline.html#asset-organization