Rails 4: 生产中未加载的资产

我试图把我的应用程序投入生产和图像和 CSS 资产路径不工作。

Here's what I'm currently doing:

  • 图像资产位于/app/asset/images/Image. jpg 中
  • 样式表位于/app/asset/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目录中的指纹文件。

当我浏览到我的网站,我得到一个404没有发现 mysite.com/stylesheets/styles.css错误。

我做错了什么?

更新: 在我的布局中,它是这样的:

<%= 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 %>

生成源如下:

<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>

看起来 Rails 没有正确地查找已编译的 css 文件。但是它非常令人困惑的 why,它对 javascript 正确工作(请注意 /assets/****.js路径)。

120693 次浏览

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

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

另外,styles.css应该在 config.assets.precompile中,config.assets.precompile是预编译的内容列表

/config/environments/production.rb中,我不得不加上以下内容:

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

那个。Js 已经被预编译了,但我还是加了进去。那个。CSS 和。显然 css.erb 不会自动发生。^[^_]不允许编译部分数据——它是一个 regexp。

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.

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

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

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

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

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

RAILS_ENV=production bundle exec rake assets:precompile

祝你好运!

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

# Rails 4:
config.serve_static_assets = false


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

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

(请参阅 Rails 5文档).正如注释中提到的,使用 Rails5,你可以只设置 RAILS_SERVE_STATIC_FILES环境变量,因为默认设置是 config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?

用于编译文件的默认匹配器包括 application.js、 application.CSS 和所有非 JS/CSS 文件(这将自动包括所有图像资产) ,它们来自包括 gems 在内的 app/asset 文件夹:

如果需要包含其他清单或单个样式表和 JavaScript 文件,可以将它们添加到 config/initializer/assets.rb 中的预编译数组:

Rails.application.config.assets.precompile += ['admin.js', 'admin.css', 'swfObject.js']

Http://guides.rubyonrails.org/asset_pipeline.html#precompiling-assets

我正在运行 Ubuntu Server 14.04Ruby 2.2.1Rails 4.2.4我已经遵循了部署 来自数字海洋的图书馆,一切进展顺利,但是当我进入浏览器并输入我的 VPS 的 IP 地址时,我的应用程序已经加载,但是没有样式和 javascript。

该应用程序与 独角兽Nginx一起运行。为了解决这个问题,我和我的用户 “部署者”一起使用 SSH 进入我的服务器,进入我的应用程序路径 主页/部署程序/应用程序/博客并运行以下命令:

RAILS_ENV=production bin/rake assets:precompile

Then I just restart the VPS and that's it! 我没问题!

希望对别人有用!

更改 Production.rb 文件行

config.assets.compile = false

进入

config.assets.compile = true

还要加上

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

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

config.assets.compile = true

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

我们的问题是,我们只在 config/secrets.yml中设置了开发秘钥库

development:
secret_key_base: '83d141eeb181032f4070ae7b1b27d9ff'

需要进入生产环境

首先检查您的资产,可能在预编译资产时有一些错误。

要在生产 ENV 中预编译资产,请运行以下命令:

RAILS_ENV=production rake assets:precompile

If it shows error, remove that first,

在出现“未定义变量”错误的情况下,加载该变量文件,然后在另一个文件中使用它。

例如:

@import "variables";
@import "style";

in application.rb file set sequence of pre-compiliation of assets

例如:

config.assets.precompile += [ 'application.js', 'admin.js', 'admin/events.js', 'admin/gallery.js', 'frontendgallery.js']


config.assets.precompile += [ 'application.css', 'admin.css','admin/events.css', 'admin/gallery.css', 'frontendgallery.css']

找到了这个:

配置选项 config.serve_static_assets已被重命名为 config.serve_static_files,以阐明其作用。

config/environments/production.rb:

# Disable serving static files from the `/public` folder by default since
# Apache or NGINX already handles this.
config.serve_static_files = ENV['RAILS_SERVE_STATIC_FILES'].present?

因此,设置 env RAILS_SERVE_STATIC_FILES或使用 Nginx来提供静态文件。 添加 config.serve_static_assets = true仍将工作,但在未来删除。

要为生产中的资产提供服务,您必须完成以下两件事:

  1. 预编译资产。
  2. 将服务器上的资产提供给浏览器。

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

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

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

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)现在,你的资产在生产服务器上,你需要把它们提供给浏览器。

Again, you have several choices.

  • 打开 < strong > config/environment/production.rb 中的 Rails 静态文件

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

    使用 Rails 服务静态文件将会扼杀 Rails 应用程序的性能。

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

    例如,我的 nginx 被配置为与 Puma 一起工作,它看起来是这样的:

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

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

config.public_file_server.enabled = true

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

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

因此,你需要设置环境变量为 true。

我可能错了,但那些建议改变的人

config.assets.compile = true

这一行的注释如下: # 如果错过了预编译的资产,不要回到资产管道。

这表明,通过将此设置为 true,您不是在修复问题,而是每次都绕过它并运行管道。这肯定会影响你的表现,破坏输油管的作用?

我有同样的错误,这是由于应用程序运行在一个子文件夹,轨道不知道。

所以我的 css 文件在 home/subfile/app/public/... 但是 Rails 在 home/app/public/..。

试着把你的应用程序从子文件夹中移出来,或者告诉 Rails 它在一个子文件夹中。

建议不要让 capistrano 进行资产预编译,因为这可能需要很长时间,而且经常会超时。尝试做本地资产预编译。

在 config/application.rb 中设置 Initialize _ on _ preedit = false 那就做本地的 RAILS _ ENV = 生产 bin/rake 资产: 预编译 and add those public/assets to git.

以及 config/Environment/development.rb,更改您的资产路径以避免使用预编译资产:

Prefix =’/dev-asset’

如果您有 db 连接问题,意味着您有使用 db 的初始化程序。解决这个问题的一种方法是通过将 production.rb 复制为 制作2.rb 来设置一个新的环境,并在 database ase.yml 中添加 制作2环境和 发展 db 设置。那就做吧

RAILS _ ENV = production2 bin/rake 资产: 预编译

如果你仍然面临一些资产问题,例如, 将 js 文件添加到 config/initializer/assets.rb 中

Preedit + =% w (ckedit or.js)

location ~ ^/assets/ {
expires 1y;
add_header Cache-Control public;
add_header ETag "";
}

This fixed the problem for me in production. Put it into the nginx config.

甚至我们也面临着同样的问题,RAILS_ENV=production bundle exec rake assets:precompile成功了,但是事情并没有像预期的那样发展。
我们发现独角兽是罪魁祸首。

和你的案子一样,我们也曾在编译完资产后重启独角兽。有人注意到,当麒麟重新启动时,只有它的辅助进程被重新启动,而不是主进程。
This is the main reason the correct assets are not served.

后来,在编译资产之后,我们停止并启动了麒麟,以便重新启动麒麟主进程,并获得正确的资产。
与重新启动独角兽相比,停止和启动独角兽会带来大约10秒的停机时间。这是解决方案,可以用在长期的解决方案是从独角兽转移到美洲狮。

你不应该做的:

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

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_ENV=production rake assets:precompile

用 rake 任务也许可以做到这一点。