ruby-on-rails - Rails 5 + Heroku : Assets are not loaded in production, 但适用于本地主机

标签 ruby-on-rails heroku ruby-on-rails-5

My problem seems like any other classic asset pipeline issues on heroku which are already asked, but none of the solutions worked for me.



问题:

如何解决这个问题呢?

当前情况:

我创建了一个自定义布局来包含我的 供应商 Assets 。下面是代码
#app/views/layouts/gentellela_theme.html.erb
<!DOCTYPE html>
<html>
  <head>
    <title>HMS</title>
    <%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
    <%= stylesheet_link_tag "css/custom", media: "all", "data-turbolinks-track" => true %>
    <%= stylesheet_link_tag "fullcalendar.print.min", :media => "print", 'data-turbolinks-track' => true %>
    <%= stylesheet_link_tag "dataTables.bootstrap4.min", media: "all", "data-turbolinks-track" => true %>
    <%= javascript_include_tag "application", "data-turbolinks-track" => true %>
    <%= javascript_include_tag "gentellela_theme", "data-turbolinks-track" => true %>
    <%= csrf_meta_tags %>

    <meta name="viewport" content="width=device-width, initial-scale=1">
  </head>

  <body class="nav-md">
    <%= render "layouts/sidenav" %>
    <%= render "layouts/topnav" %>

    <%= yield %>
    <%= render "layouts/footer" %>
    <%= javascript_include_tag 'js/custom', 'data-turbolinks-track' => true %>
  </body>
</html>

问题:

我在 vendor/assets/components/gentelella/production/js/custom.jsvendor/assets/components/gentelella/production/css/custom.css 下有两个文件。这些文件在本地主机中加载并运行良好,但它们是 未加载到 Heroku

我只能在浏览器控制台中看到如下所示的警告,但仅此而已。 也没有 404 错误。

Loading failed for the with source “https://boiling-dusk-64956.herokuapp.com/javascripts/js/custom.js



我的 config/application.rb 看起来像这样
require_relative 'boot'

require 'rails/all'

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

module HMS
  class Application < Rails::Application
    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration should go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded.
    config.assets.paths << Rails.root.join('vendor', 'assets', 'components', 'gentelella', 'production')
  end
end

我的 config/initializers/assets.rb 看起来像这样
# Be sure to restart your server when you modify this file.

# Version of your assets, change this if you want to expire all your assets.
Rails.application.config.assets.version = '1.0'

# Add additional assets to the asset load path
# Rails.application.config.assets.paths << Emoji.images_path

# Precompile additional assets.
# application.js, application.css, and all non-JS/CSS in app/assets folder are already added.
# Rails.application.config.assets.precompile += %w( search.js )
Rails.application.config.assets.precompile += %w( fullcalendar.print.min.css )
Rails.application.config.assets.precompile += %w( dataTables.bootstrap4.min.css)
Rails.application.config.assets.precompile += %w( gentellela_theme.js )
Rails.application.config.assets.precompile << Proc.new { |path, fn| fn =~ /vendor\/assets\/components\/gentelella\/production/ }

所以我引用了相关问题并试图解决问题,但无济于事。以下是我失败的尝试

尝试 #1

在本地预编译 Assets
RAILS_ENV=production bundle exec rake assets:precompile

并将更改推送到heroku,但问题仍然存在。

尝试 #2

config/application.rb 中添加了以下几行
config.assets.enabled = true
config.serve_static_assets = true

并将更改推送到heroku,但问题仍然存在。

尝试 #3

config.assets.compile = false 中的 config.assets.compile = true 修改为 config/environments/production.rb ,但是推送到 heroku 时,推送失败,出现以下错误
remote:  !     A security vulnerability has been detected in your application.
remote:  !     To protect your application you must take action. Your application
remote:  !     is currently exposing its credentials via an easy to exploit directory
remote:  !     traversal.
remote:  !     
remote:  !     To protect your application you must either upgrade to Sprockets version "3.7.2"
remote:  !     or disable dynamic compilation at runtime by setting:
remote:  !     
remote:  !     ```
remote:  !     config.assets.compile = false # Disables security vulnerability
remote:  !     ```
remote:  !     
remote:  !     To read more about this security vulnerability please refer to this blog post:
remote:  !     https://blog.heroku.com/rails-asset-pipeline-vulnerability
remote:  !
remote:  !     Push rejected, failed to compile Ruby app.
remote: 
remote:  !     Push failed
remote: Verifying deploy...
remote: 
remote: !   Push rejected to boiling-dusk-64956.

因此,我按照建议升级了链轮版本,并将更改推送到了 heroku。这次推送成功了,没有任何错误。接下来,我运行了 heroku run rake assets:precomplie 并重新启动了服务器。但是现在应用程序崩溃了。下面是 heroku logs --tail 信息
2018-09-20T08:27:33.161369+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=GET path="/departments" host=boiling-dusk-64956.herokuapp.com request_id=fe446dfc-ea7c-4d38-8b6b-ec529e8db9eb fwd="27.7.94.242" dyno=web.1 connect=1ms service=30000ms status=503 bytes=0 protocol=https
2018-09-20T08:28:34.114593+00:00 heroku[web.1]: Process running mem=577M(112.7%)
2018-09-20T08:28:34.114685+00:00 heroku[web.1]: Error R14 (Memory quota exceeded)
2018-09-20T08:29:16.452991+00:00 heroku[web.1]: Error R14 (Memory quota exceeded)
2018-09-20T08:29:16.452911+00:00 heroku[web.1]: Process running mem=611M(119.5%)
2018-09-20T08:30:16.740081+00:00 heroku[web.1]: Restarting
2018-09-20T08:30:16.743963+00:00 heroku[web.1]: State changed from up to starting
2018-09-20T08:30:17.601536+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2018-09-20T08:30:17.631815+00:00 app[web.1]: [2018-09-20 08:30:17] FATAL SignalException: SIGTERM
2018-09-20T08:30:17.631835+00:00 app[web.1]: /app/vendor/ruby-2.3.7/lib/ruby/2.3.0/webrick/server.rb:177:in `select'
2018-09-20T08:30:17.631838+00:00 app[web.1]: /app/vendor/ruby-2.3.7/lib/ruby/2.3.0/webrick/server.rb:177:in `block in start'
2018-09-20T08:30:17.631840+00:00 app[web.1]: /app/vendor/ruby-2.3.7/lib/ruby/2.3.0/webrick/server.rb:33:in `start'
2018-09-20T08:30:17.631843+00:00 app[web.1]: /app/vendor/bundle/ruby/2.3.0/gems/rack-2.0.5/lib/rack/handler/webrick.rb:34:in `run'
2018-09-20T08:30:17.631841+00:00 app[web.1]: /app/vendor/ruby-2.3.7/lib/ruby/2.3.0/webrick/server.rb:164:in `start'
2018-09-20T08:30:17.631847+00:00 app[web.1]: /app/vendor/bundle/ruby/2.3.0/gems/railties-5.0.0/lib/rails/commands/server.rb:79:in `start'
2018-09-20T08:30:17.631845+00:00 app[web.1]: /app/vendor/bundle/ruby/2.3.0/gems/rack-2.0.5/lib/rack/server.rb:297:in `start'
2018-09-20T08:30:17.631851+00:00 app[web.1]: /app/vendor/bundle/ruby/2.3.0/gems/railties-5.0.0/lib/rails/commands/commands_tasks.rb:85:in `tap'
2018-09-20T08:30:17.631849+00:00 app[web.1]: /app/vendor/bundle/ruby/2.3.0/gems/railties-5.0.0/lib/rails/commands/commands_tasks.rb:90:in `block in server'
2018-09-20T08:30:17.631853+00:00 app[web.1]: /app/vendor/bundle/ruby/2.3.0/gems/railties-5.0.0/lib/rails/commands/commands_tasks.rb:85:in `server'
2018-09-20T08:30:17.631856+00:00 app[web.1]: /app/vendor/bundle/ruby/2.3.0/gems/railties-5.0.0/lib/rails/commands.rb:18:in `<top (required)>'
2018-09-20T08:30:17.631859+00:00 app[web.1]: bin/rails:4:in `require'
2018-09-20T08:30:17.631854+00:00 app[web.1]: /app/vendor/bundle/ruby/2.3.0/gems/railties-5.0.0/lib/rails/commands/commands_tasks.rb:49:in `run_command!'
2018-09-20T08:30:17.697233+00:00 app[web.1]: [2018-09-20 08:30:17] INFO  WEBrick::HTTPServer#start done.
2018-09-20T08:30:17.631860+00:00 app[web.1]: bin/rails:4:in `<main>'
2018-09-20T08:30:17.632626+00:00 app[web.1]: [2018-09-20 08:30:17] INFO  going to shutdown ...
2018-09-20T08:30:19.862800+00:00 app[web.1]: I, [2018-09-20T08:30:17.637116 #4]  INFO -- : [fe446dfc-ea7c-4d38-8b6b-ec529e8db9eb] Completed 500 Internal Server Error in 194442ms (ActiveRecord: 2.6ms)
2018-09-20T08:30:19.862814+00:00 app[web.1]: F, [2018-09-20T08:30:17.643023 #4] FATAL -- : [fe446dfc-ea7c-4d38-8b6b-ec529e8db9eb]
2018-09-20T08:30:19.862817+00:00 app[web.1]: F, [2018-09-20T08:30:17.643123 #4] FATAL -- : [fe446dfc-ea7c-4d38-8b6b-ec529e8db9eb] ActionView::Template::Error ():
2018-09-20T08:30:19.862820+00:00 app[web.1]: F, [2018-09-20T08:30:17.647307 #4] FATAL -- : [fe446dfc-ea7c-4d38-8b6b-ec529e8db9eb]     3:    <head>
2018-09-20T08:30:19.862822+00:00 app[web.1]: F, [2018-09-20T08:30:17.647399 #4] FATAL -- : [fe446dfc-ea7c-4d38-8b6b-ec529e8db9eb]     4:      <title>HMS</title>
2018-09-20T08:30:19.862824+00:00 app[web.1]: F, [2018-09-20T08:30:17.647458 #4] FATAL -- : [fe446dfc-ea7c-4d38-8b6b-ec529e8db9eb]     5:     <%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
2018-09-20T08:30:19.862826+00:00 app[web.1]: F, [2018-09-20T08:30:17.647517 #4] FATAL -- : [fe446dfc-ea7c-4d38-8b6b-ec529e8db9eb]     6:     <%= stylesheet_link_tag "css/custom", media: "all", "data-turbolinks-track" => true %>
2018-09-20T08:30:19.862829+00:00 app[web.1]: F, [2018-09-20T08:30:17.647561 #4] FATAL -- : [fe446dfc-ea7c-4d38-8b6b-ec529e8db9eb]     7:     <%= stylesheet_link_tag "fullcalendar.print.min", :media => "print", 'data-turbolinks-track' => true %>
2018-09-20T08:30:19.862831+00:00 app[web.1]: F, [2018-09-20T08:30:17.647604 #4] FATAL -- : [fe446dfc-ea7c-4d38-8b6b-ec529e8db9eb]     8:     <%= stylesheet_link_tag "dataTables.bootstrap4.min", media: "all", "data-turbolinks-track" => true %>
2018-09-20T08:30:19.862833+00:00 app[web.1]: F, [2018-09-20T08:30:17.647647 #4] FATAL -- : [fe446dfc-ea7c-4d38-8b6b-ec529e8db9eb]     9:      <%= javascript_include_tag "application", "data-turbolinks-track" => true %>
2018-09-20T08:30:19.862835+00:00 app[web.1]: F, [2018-09-20T08:30:17.647690 #4] FATAL -- : [fe446dfc-ea7c-4d38-8b6b-ec529e8db9eb]
2018-09-20T08:30:19.862838+00:00 app[web.1]: F, [2018-09-20T08:30:17.647732 #4] FATAL -- : [fe446dfc-ea7c-4d38-8b6b-ec529e8db9eb] app/views/layouts/gentellela_theme.html.erb:6:in `_app_views_layouts_gentellela_theme_html_erb___843058433631945669_70285598017940'
2018-09-20T08:30:19.862840+00:00 app[web.1]: I, [2018-09-20T08:30:17.657292 #4]  INFO -- : [ab0d5f1c-d91e-4741-825e-97e1ce5443cf] Completed 500 Internal Server Error in 271041ms (ActiveRecord: 38.3ms)
2018-09-20T08:30:19.862841+00:00 app[web.1]: F, [2018-09-20T08:30:17.676144 #4] FATAL -- : [ab0d5f1c-d91e-4741-825e-97e1ce5443cf]
2018-09-20T08:30:19.862843+00:00 app[web.1]: F, [2018-09-20T08:30:17.676296 #4] FATAL -- : [ab0d5f1c-d91e-4741-825e-97e1ce5443cf] ActionView::Template::Error ():
2018-09-20T08:30:19.862845+00:00 app[web.1]: F, [2018-09-20T08:30:17.679494 #4] FATAL -- : [ab0d5f1c-d91e-4741-825e-97e1ce5443cf]     3:    <head>
2018-09-20T08:30:19.862847+00:00 app[web.1]: F, [2018-09-20T08:30:17.679561 #4] FATAL -- : [ab0d5f1c-d91e-4741-825e-97e1ce5443cf]     4:      <title>HMS</title>
2018-09-20T08:30:19.862848+00:00 app[web.1]: F, [2018-09-20T08:30:17.679834 #4] FATAL -- : [ab0d5f1c-d91e-4741-825e-97e1ce5443cf]     5:     <%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
2018-09-20T08:30:19.862850+00:00 app[web.1]: F, [2018-09-20T08:30:17.679898 #4] FATAL -- : [ab0d5f1c-d91e-4741-825e-97e1ce5443cf]     6:     <%= stylesheet_link_tag "css/custom", media: "all", "data-turbolinks-track" => true %>
2018-09-20T08:30:19.862852+00:00 app[web.1]: F, [2018-09-20T08:30:17.680159 #4] FATAL -- : [ab0d5f1c-d91e-4741-825e-97e1ce5443cf]     7:     <%= stylesheet_link_tag "fullcalendar.print.min", :media => "print", 'data-turbolinks-track' => true %>
2018-09-20T08:30:19.862853+00:00 app[web.1]: F, [2018-09-20T08:30:17.680220 #4] FATAL -- : [ab0d5f1c-d91e-4741-825e-97e1ce5443cf]     8:     <%= stylesheet_link_tag "dataTables.bootstrap4.min", media: "all", "data-turbolinks-track" => true %>
2018-09-20T08:30:19.862855+00:00 app[web.1]: F, [2018-09-20T08:30:17.680277 #4] FATAL -- : [ab0d5f1c-d91e-4741-825e-97e1ce5443cf]     9:      <%= javascript_include_tag "application", "data-turbolinks-track" => true %>
2018-09-20T08:30:19.862857+00:00 app[web.1]: F, [2018-09-20T08:30:17.683960 #4] FATAL -- : [ab0d5f1c-d91e-4741-825e-97e1ce5443cf]
2018-09-20T08:30:19.862859+00:00 app[web.1]: F, [2018-09-20T08:30:17.684030 #4] FATAL -- : [ab0d5f1c-d91e-4741-825e-97e1ce5443cf] app/views/layouts/gentellela_theme.html.erb:6:in `_app_views_layouts_gentellela_theme_html_erb___843058433631945669_70285598017940'
2018-09-20T08:30:19.862861+00:00 app[web.1]: Exiting
2018-09-20T08:30:20.300416+00:00 heroku[web.1]: Process exited with status 143
2018-09-20T08:30:26.455831+00:00 heroku[web.1]: Starting process with command `bin/rails server -p 51256 -e production`
2018-09-20T08:30:32.965279+00:00 app[web.1]: The PGconn, PGresult, and PGError constants are deprecated, and will be
2018-09-20T08:30:32.965301+00:00 app[web.1]: removed as of version 1.0.
2018-09-20T08:30:32.965303+00:00 app[web.1]: 
2018-09-20T08:30:32.965305+00:00 app[web.1]: You should use PG::Connection, PG::Result, and PG::Error instead, respectively.
2018-09-20T08:30:32.965306+00:00 app[web.1]: 
2018-09-20T08:30:32.965309+00:00 app[web.1]: Called from /app/vendor/bundle/ruby/2.3.0/gems/activesupport-5.0.0/lib/active_support/dependencies.rb:259:in `load_dependency'
2018-09-20T08:30:33.458524+00:00 app[web.1]: [2018-09-20 08:30:33] INFO  WEBrick 1.3.1
2018-09-20T08:30:33.458570+00:00 app[web.1]: [2018-09-20 08:30:33] INFO  ruby 2.3.7 (2018-03-28) [x86_64-linux]
2018-09-20T08:30:33.459164+00:00 app[web.1]: [2018-09-20 08:30:33] INFO  WEBrick::HTTPServer#start: pid=4 port=51256
2018-09-20T08:30:33.797414+00:00 heroku[web.1]: State changed from starting to up

因此,我无法检测应用程序崩溃的原因。我是否在正确的 rails 上解决我的问题?任何帮助表示赞赏。 TIA

更新 #1:

我让我的应用程序运行并遵循 @Maxence 方法。现在我可以看到两个文件的 404 错误消息
ActionController::RoutingError (No route matches [GET] "/stylesheets/css/custom.css") 

ActionController::RoutingError (No route matches [GET] "/javascripts/js/custom.js")

更新 #2:
sprockets-manifest 文件中这些文件的逻辑路径如下
gentelella/production/js/custom.js


gentelella/production/css/custom.css

最佳答案

好吧,我和你一样在生产中遇到了很多 Assets 管道问题。
第一:不要在 Heroku 编译 Assets 。是否通过CLI做heroku run rake assets:precomplie或通过您的生产文件 config.assets.compile = true .它也没有为我工作。
只需使用推荐的 config.assets.compile = false .它还将使您的部署更快,因为所有内容都已在本地预编译,并且您的 slug 也会更薄。
所以基本上只做一个 RAILS_ENV=production bundle exec rake assets:precompile本地。这将在您的 public/assets 中创建指纹 Assets 。文件夹。它还将创建一个 .sprockets-manifest此文件夹中的文件。sprockets_manifest是您的指纹 Assets 与应用助手引用的非指纹 Assets 的对应表image_tag等等..(你可以打开 list 文件,它很有趣,帮助我理解了我遇到的问题)
现在您应该能够看到您在 public/assets 中指纹识别的所有 Assets 。文件夹。如果有些丢失,您应该检查您的 assets.rb文件并确保您已包含所需的所有文件。 (预编译方法只预编译本文件中提到的内容)
基本上从现在开始,您的应用程序很好,尽管我发现了一些可能会阻止 list 文件/链轮良好匹配 Assets 的事情:

  • 在你的助手中,总是提到文件类型 image_tag(splash)应改为 image_tag(splash.jpg) .这是因为您的 list 文件将文件类型添加到逻辑路径中,而 rails 不会自动猜测它。你应该对 javascript_include_tag "gentellela_theme" 做同样的事情并将其更改为 javascript_include_tag "gentellela_theme.js" .或 javascript_include_tag 'js/custom'javascript_include_tag 'js/custom.js'
  • 如果您使用 SASS,请确保您的 css 文件正确命名 whatever.css.scss而不是 whatever.scss .我在我的 css 文件中使用了 sass 助手,我的文件命名错误破坏了 list 的匹配。

  • 那你应该很好
    编辑
    根据下面的评论,如果您已将 Assets 名称更改为全名+文件类型(例如 splash 更改为 splash.jpg)并且文件仍未显示。检查 list 文件中该特定 Assets 的逻辑路径。并用逻辑路径替换 Assets 的名称。逻辑路径消除了 Assets 名称的歧义。
    编辑 2
    该线程只涉及链轮 3.X。 Sprockets 4/Webpacker 的工作方式不同

    关于ruby-on-rails - Rails 5 + Heroku : Assets are not loaded in production, 但适用于本地主机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52421137/

    相关文章:

    ruby-on-rails - 在 Rails 中发送电子邮件时事通讯/调查

    ruby-on-rails-3 - 如何在 Heroku 上自动缩放网络测功机?

    ruby-on-rails - 在未调用的辅助方法中打开 CSV 文件

    ruby-on-rails - rails 5 : find_or_create_by not saving all the params

    ruby-on-rails - Rails 5,Rspec : Environment data not found in the schema

    ruby-on-rails - 如何自定义 Capistranos SSH 命令?

    ruby-on-rails - 如何在重新请求作业完成时提醒用户

    javascript - 为什么此表单不断提交过时的参数?

    node.js - Cloudflare + Heroku = 免费 SSL - 无法正常工作

    html - 如何在 Rails 中为 datetime_field_tag 设置默认值