Ruby-on-rails Rails 4:资产未在生产中加载

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

Rails 4: assets not loading in production

ruby-on-railsrubyasset-pipelineruby-on-rails-4

提问by emersonthis

I'm trying to put my app into production and image and css asset paths aren't working.

我正在尝试将我的应用程序投入生产,但图像和 css 资产路径不起作用。

Here's what I'm currently doing:

这是我目前正在做的事情:

  • Image assets live in /app/assets/images/image.jpg
  • Stylesheets live in /app/assets/stylesheets/style.css
  • In my layout, I reference the css file like this: <%= stylesheet_link_tag "styles", media: "all", "data-turbolinks-track" => true %>
  • Before restarting unicorn, I run RAILS_ENV=production bundle exec rake assets:precompileand it succeeds and I see the fingerprinted files in the public/assetsdirectory.
  • 图像资产位于 /app/assets/images/image.jpg
  • 样式表位于 /app/assets/stylesheets/style.css
  • 在我的布局中,我像这样引用 css 文件: <%= stylesheet_link_tag "styles", media: "all", "data-turbolinks-track" => true %>
  • 在重新启动 unicorn 之前,我运行RAILS_ENV=production bundle exec rake assets:precompile并成功并在public/assets目录中看到了指纹文件。

When I browse to my site, I get a 404 not found error for mysite.com/stylesheets/styles.css.

当我浏览我的网站时,我收到 404 not found 错误mysite.com/stylesheets/styles.css

What am I doing wrong?

我究竟做错了什么?

Update:In my layout, it looks like this:

更新:在我的布局中,它看起来像这样:

<%= stylesheet_link_tag    "bootstrap.min", media: "all", "data-turbolinks-track" => true %>
<%= stylesheet_link_tag    "styles", media: "all", "data-turbolinks-track" => true %>
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>

The generate source is this:

生成源是这样的:

<link data-turbolinks-track="true" href="/stylesheets/bootstrap.min.css" media="all" rel="stylesheet" />
<link data-turbolinks-track="true" href="/stylesheets/styles.css" media="all" rel="stylesheet" />
<script data-turbolinks-track="true" src="/assets/application-0c647c942c6eff10ad92f1f2b0c64efe.js"></script>

Looks like Rails is not properly looking for the compiled css files. But it's very confusing whyit's working correctly for javascripts (notice the /assets/****.jspath).

看起来 Rails 没有正确查找已编译的 css 文件。但是对于javascripts为什么它可以正常工作非常令人困惑(注意/assets/****.js路径)。

采纳答案by emersonthis

In /config/environments/production.rbI had to add this:

/config/environments/production.rb我不得不添加这个:

Rails.application.config.assets.precompile += %w( *.js ^[^_]*.css *.css.erb )

The .js was getting precompiled already, but I added it anyway. The .css and .css.erb apparently don't happen automatically. The ^[^_]excludes partials from being compiled -- it's a regexp.

.js 已经被预编译了,但我还是添加了它。.css 和 .css.erb 显然不会自动发生。在^[^_]不包括来自谐音被编译-这是一个正则表达式。

It's a little frustrating that the docs clearly state that asset pipeline IS enabled by default but doesn't clarify the fact that only applies to javascripts.

有点令人沮丧的是,文档明确指出默认情况下启用资产管道,但没有澄清仅适用于 javascripts 的事实。

回答by Rameshwar Vyevhare

In rails 4 you need to make the changes below:

在 rails 4 中,您需要进行以下更改:

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

This works with me. use following command to pre-compile assets

这对我有用。使用以下命令预编译资产

RAILS_ENV=production bundle exec rake assets:precompile

Best of luck!

祝你好运!

回答by davmac

I just had the same problem and found this setting in config/environments/production.rb:

我刚刚遇到了同样的问题,并在 config/environments/production.rb 中找到了这个设置:

# Rails 4:
config.serve_static_assets = false

# Or for Rails 5:
config.public_file_server.enabled = false

Changing it to truegot it working. It seems by default Rails expects you to have configured your front-end webserver to handle requests for files out of the public folder instead of proxying them to the Rails app. Perhaps you've done this for your javascript files but not your CSS stylesheets?

改变它true让它工作。默认情况下,Rails 似乎希望您配置前端 Web 服务器来处理对公共文件夹之外的文件的请求,而不是将它们代理到 Rails 应用程序。也许您已经为您的 javascript 文件而不是您的 CSS 样式表完成了这项工作?

(See Rails 5 documentation). As noted in comments, with Rails 5 you may just set the RAILS_SERVE_STATIC_FILESenvironment variable, since the default setting is config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?.

请参阅 Rails 5 文档)。如评论中所述,在 Rails 5 中,您可以只设置RAILS_SERVE_STATIC_FILES环境变量,因为默认设置是config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?.

回答by Yanofsky

I was able to solve this problem by changing: config.assets.compile = falseto
config.assets.compile = truein /config/environments/production.rb

我能够通过改变来解决这个问题: config.assets.compile = false
config.assets.compile = true/config/environments/production.rb

Update (June 24, 2018): This method creates a security vulnerabilityif the version of Sprockets you're using is less than 2.12.5, 3.7.2, or 4.0.0.beta8

更新(2018 年 6 月 24 日):如果您使用的链轮版本低于 2.12.5、3.7.2 或 4.0.0.beta8,此方法会产生安全漏洞

回答by ytbryan

For Rails 5, you should enable the follow config code:

对于 Rails 5,您应该启用以下配置代码:

config.public_file_server.enabled = true

config.public_file_server.enabled = true

By default, Rails 5 ships with this line of config:

默认情况下,Rails 5 附带这行配置:

config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?

config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?

Hence, you will need to set the environment variable RAILS_SERVE_STATIC_FILESto true.

因此,您需要将环境变量设置RAILS_SERVE_STATIC_FILES为 true。

回答by Chau H?ng L?nh

There are 2 things you must accomplish to serve the assets in production:

您必须完成两件事才能为生产中的资产提供服务:

  1. Precompile the assets.
  2. Serve the assets on the server to browser.
  1. 预编译资产。
  2. 将服务器上的资产提供给浏览器。

1) In order to precompile the assets, you have several choices.

1) 为了预编译资产,您有多种选择。

  • You can run rake assets:precompileon your local machine, commit it to source code control (git), then run the deployment program, for example capistrano. This is not a good way to commit precompiled assets to SCM.

  • You can write a rake task that run RAILS_ENV=production rake assets:precompileon the target servers each time you deploy your Rails app to production, before you restart the server.

  • 您可以rake assets:precompile在本地机器上运行,将其提交到源代码控制 (git),然后运行部署程序,例如 capistrano。这不是将预编译资产提交给 SCM 的好方法。

  • RAILS_ENV=production rake assets:precompile每次将 Rails 应用程序部署到生产环境时,您都可以编写一个在目标服务器上运行的 rake 任务,然后再重新启动服务器。

Code in a task for capistrano will look similar to this:

capistrano 任务中的代码将类似于以下内容:

on roles(:app) do
  if DEPLOY_ENV == 'production'
    execute("cd #{DEPLOY_TO_DIR}/current && RAILS_ENV=production rvm #{ruby_string} do rake assets:precompile")
  end
end

2) Now, you have the assets on production servers, you need to serve them to browser.

2)现在,您在生产服务器上拥有资产,您需要将它们提供给浏览器。

Again, you have several choices.

同样,您有多种选择。

  • Turn on Rails static file serving in config/environments/production.rb

    config.serve_static_assets = true # old
    
    or
    
    config.serve_static_files = true # new
    

    Using Rails to serve static files will kill your Rails app performance.

  • Configure nginx (or Apache) to serve static files.

    For example, my nginx that was configured to work with Puma looks like this:

    location ~ ^/(assets|images|fonts)/(.*)$ {
        alias /var/www/foster_care/current/public//;
        gzip on;
        expires max;
        add_header Cache-Control public;
    }
    
  • config/environments/production.rb 中开启 Rails 静态文件服务

    config.serve_static_assets = true # old
    
    or
    
    config.serve_static_files = true # new
    

    使用 Rails 提供静态文件会降低 Rails 应用程序的性能。

  • 配置 nginx(或 Apache)以提供静态文件。

    例如,我配置为与 Puma 一起使用的 nginx 如下所示:

    location ~ ^/(assets|images|fonts)/(.*)$ {
        alias /var/www/foster_care/current/public//;
        gzip on;
        expires max;
        add_header Cache-Control public;
    }
    

回答by Frederick Cheung

Rails 4 no longer generates the non fingerprinted version of the asset: stylesheets/style.css will not be generated for you.

Rails 4 不再生成资产的非指纹版本:将不会为您生成 stylesheets/style.css。

If you use stylesheet_link_tagthen the correct link to your stylesheet will be generated

如果您使用,stylesheet_link_tag则将生成指向您的样式表的正确链接

In addition styles.cssshould be in config.assets.precompilewhich is the list of things that are precompiled

另外styles.css应该在config.assets.precompile哪些是预编译的东西列表

回答by Jassa Mahal

change your Production.rb file line

更改您的 Production.rb 文件行

config.assets.compile = false

into

进入

config.assets.compile = true

and also add

并添加

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

回答by BKSpurgeon

What you SHOULD NOT do:

你不应该做什么:

Some of my colleagues above have recommended you to do this:

我上面的一些同事建议你这样做:

config.serve_static_assets = true  ## DON”T DO THIS!! 
config.public_file_server.enabled = true ## DON”T DO THIS!!

The rails asset pipeline says of the above approach:

rails 资产管道说明了上述方法:

This mode uses more memory, performs more poorly than the default and is not recommended. See here: (http://edgeguides.rubyonrails.org/asset_pipeline.html#live-compilation)

此模式使用更多内存,性能比默认模式更差,不推荐使用。请参阅此处:(http://edgeguides.rubyonrails.org/asset_pipeline.html#live-compilation

What you SHOULD do:

你应该做什么:

Precompile your assets.

预编译您的资产。

RAILS_ENV=production rake assets:precompile

RAILS_ENV=production rake assets:precompile

You can probably do that with a rake task.

您可能可以通过 rake 任务来做到这一点。

回答by xxjjnn

If precompile is set you DO NOT need

如果设置了预编译,则不需要

config.assets.compile = true

as this is to serve assets live.

因为这是为资产提供现场服务。

Our problem was we only had development secret key base set in config/secrets.yml

我们的问题是我们只设置了开发密钥库 config/secrets.yml

development:
    secret_key_base: '83d141eeb181032f4070ae7b1b27d9ff'

Need entry for production environment

需要进入生产环境