预编译Rails 5以包含所有资产

我正在尝试预编译我的所有资产以进行生产。 当我运行RAILS_ENV=production bundle exec rake assets:precompile并非所有资产都是预编译的。 我试图使用别人在其他类似问题中建议的方法,但它们对我没用。 对于初学者来说,这是我的assets.rb看起来像:

 Rails.application.config.assets.version = '1.0' Rails.application.config.assets.paths << Rails.root.join('node_modules') Rails.application.config.assets.precompile += %w(creative/manifest.js creative/manifest.css images/* bootstrap/* fonts/* magnific-popup/* morphext/* owlcarousel2/* rs-plugin-5/* stylesheets/* javascripts/*) 

正如您可以看到我预编译的位置,我已尝试添加示例directory/*以包含所有内容。 以下是我试图包含的文件及其中的所有内容(这些文件位于Assets目录中):

 bootstrap fonts javascripts morphext rs-plugin-5 config images magnific-popup owlcarousel2 stylesheets 

这是在我的公共目录中进行预编译时运行预编译时看一下公共图像,你可以看到许多目录不在那里我甚至打开了每个文件夹,看看它们是否在另一个文件夹中并没有什么是存在的:

公共目录

最后一部分是当我运行rails s -e production并导航到Localhost:3000我一直收到500错误,没有显示任何内容。 在chrome和终端的控制台中没有错误,所以我无法看到我的页面在本地生产的样子。 任何帮助将不胜感激。

更新

这是我的production.log看起来它没有得到必要的资产:

 I, [2017-09-12T18:43:54.606853 #7393] INFO -- : [b2f4fb53-c1ea-4d59-8045-44167c60416c] Started GET "/" for 127.0.0.1 at 2017-09-12 18:43:54 -0700 I, [2017-09-12T18:43:54.625461 #7393] INFO -- : [b2f4fb53-c1ea-4d59-8045-44167c60416c] Processing by CreativesController#index as HTML I, [2017-09-12T18:43:54.642161 #7393] INFO -- : [b2f4fb53-c1ea-4d59-8045-44167c60416c] Rendering creatives/index.html.erb within layouts/application I, [2017-09-12T18:43:54.644418 #7393] INFO -- : [b2f4fb53-c1ea-4d59-8045-44167c60416c] Rendered creatives/index.html.erb within layouts/application (2.1ms) I, [2017-09-12T18:43:54.644629 #7393] INFO -- : [b2f4fb53-c1ea-4d59-8045-44167c60416c] Completed 500 Internal Server Error in 19ms F, [2017-09-12T18:43:54.645433 #7393] FATAL -- : [b2f4fb53-c1ea-4d59-8045-44167c60416c] F, [2017-09-12T18:43:54.645478 #7393] FATAL -- : [b2f4fb53-c1ea-4d59-8045-44167c60416c] ActionView::Template::Error (The asset "AdobeStock_95578405.jpeg" is not present in the asset pipeline.): F, [2017-09-12T18:43:54.645761 #7393] FATAL -- : [b2f4fb53-c1ea-4d59-8045-44167c60416c] 17: 
  • [b2f4fb53-c1ea-4d59-8045-44167c60416c] 18: [b2f4fb53-c1ea-4d59-8045-44167c60416c] 19: [b2f4fb53-c1ea-4d59-8045-44167c60416c] 20: {bgposition: "center bottom",bgrepeat: "no-repeat", bgfit: "cover"} , :class => "rev-slidebg" %> [b2f4fb53-c1ea-4d59-8045-44167c60416c] 21: [b2f4fb53-c1ea-4d59-8045-44167c60416c] 22: [b2f4fb53-c1ea-4d59-8045-44167c60416c] 23: <div class="tp-caption dark-translucent-bg" F, [2017-09-12T18:43:54.645799 #7393] FATAL -- : [b2f4fb53-c1ea-4d59-8045-44167c60416c] F, [2017-09-12T18:43:54.645828 #7393] FATAL -- : [b2f4fb53-c1ea-4d59-8045-44167c60416c] app/views/creatives/index.html.erb:20:in `_app_views_creatives_index_html_erb___200307797594027740_70304554688000' I, [2017-09-12T18:43:54.734601 #7393] INFO -- : [8f353cbb-4884-4c2c-896d-ba60b58caa61] Started GET "/favicon.ico" for 127.0.0.1 at 2017-09-12 18:43:54 -0700 F, [2017-09-12T18:43:54.735010 #7393] FATAL -- : [8f353cbb-4884-4c2c-896d-ba60b58caa61] F, [2017-09-12T18:43:54.735048 #7393] FATAL -- : [8f353cbb-4884-4c2c-896d-ba60b58caa61] ActionController::RoutingError (No route matches [GET] "/favicon.ico"): F, [2017-09-12T18:43:54.735071 #7393] FATAL -- : [8f353cbb-4884-4c2c-896d-ba60b58caa61] F, [2017-09-12T18:43:54.735100 #7393] FATAL -- : [8f353cbb-4884-4c2c-896d-ba60b58caa61] actionpack (5.1.3) lib/action_dispatch/middleware/debug_exceptions.rb:63:in `call' [8f353cbb-4884-4c2c-896d-ba60b58caa61] actionpack (5.1.3) lib/action_dispatch/middleware/show_exceptions.rb:31:in `call' [8f353cbb-4884-4c2c-896d-ba60b58caa61] railties (5.1.3) lib/rails/rack/logger.rb:36:in `call_app' [8f353cbb-4884-4c2c-896d-ba60b58caa61] railties (5.1.3) lib/rails/rack/logger.rb:24:in `block in call' [8f353cbb-4884-4c2c-896d-ba60b58caa61] activesupport (5.1.3) lib/active_support/tagged_logging.rb:69:in `block in tagged' [8f353cbb-4884-4c2c-896d-ba60b58caa61] activesupport (5.1.3) lib/active_support/tagged_logging.rb:26:in `tagged' [8f353cbb-4884-4c2c-896d-ba60b58caa61] activesupport (5.1.3) lib/active_support/tagged_logging.rb:69:in `tagged' [8f353cbb-4884-4c2c-896d-ba60b58caa61] railties (5.1.3) lib/rails/rack/logger.rb:24:in `call' [8f353cbb-4884-4c2c-896d-ba60b58caa61] actionpack (5.1.3) lib/action_dispatch/middleware/remote_ip.rb:79:in `call' [8f353cbb-4884-4c2c-896d-ba60b58caa61] actionpack (5.1.3) lib/action_dispatch/middleware/request_id.rb:25:in `call' [8f353cbb-4884-4c2c-896d-ba60b58caa61] rack (2.0.3) lib/rack/method_override.rb:22:in `call' [8f353cbb-4884-4c2c-896d-ba60b58caa61] rack (2.0.3) lib/rack/runtime.rb:22:in `call' [8f353cbb-4884-4c2c-896d-ba60b58caa61] activesupport (5.1.3) lib/active_support/cache/strategy/local_cache_middleware.rb:27:in `call' [8f353cbb-4884-4c2c-896d-ba60b58caa61] actionpack (5.1.3) lib/action_dispatch/middleware/executor.rb:12:in `call' [8f353cbb-4884-4c2c-896d-ba60b58caa61] rack (2.0.3) lib/rack/sendfile.rb:111:in `call' [8f353cbb-4884-4c2c-896d-ba60b58caa61] railties (5.1.3) lib/rails/engine.rb:522:in `call' [8f353cbb-4884-4c2c-896d-ba60b58caa61] puma (3.10.0) lib/puma/configuration.rb:225:in `call' [8f353cbb-4884-4c2c-896d-ba60b58caa61] puma (3.10.0) lib/puma/server.rb:605:in `handle_request' [8f353cbb-4884-4c2c-896d-ba60b58caa61] puma (3.10.0) lib/puma/server.rb:437:in `process_client' [8f353cbb-4884-4c2c-896d-ba60b58caa61] puma (3.10.0) lib/puma/server.rb:301:in `block in run' [8f353cbb-4884-4c2c-896d-ba60b58caa61] puma (3.10.0) lib/puma/thread_pool.rb:120:in `block in spawn_thread' I, [2017-09-12T18:43:58.249911 #7393] INFO -- : [2e6342ac-ec61-410c-98ba-ae205fb9cc26] Started GET "/" for 127.0.0.1 at 2017-09-12 18:43:58 -0700 I, [2017-09-12T18:43:58.251066 #7393] INFO -- : [2e6342ac-ec61-410c-98ba-ae205fb9cc26] Processing by CreativesController#index as HTML I, [2017-09-12T18:43:58.252212 #7393] INFO -- : [2e6342ac-ec61-410c-98ba-ae205fb9cc26] Rendering creatives/index.html.erb within layouts/application I, [2017-09-12T18:43:58.253079 #7393] INFO -- : [2e6342ac-ec61-410c-98ba-ae205fb9cc26] Rendered creatives/index.html.erb within layouts/application (0.7ms) I, [2017-09-12T18:43:58.253250 #7393] INFO -- : [2e6342ac-ec61-410c-98ba-ae205fb9cc26] Completed 500 Internal Server Error in 2ms F, [2017-09-12T18:43:58.254061 #7393] FATAL -- : [2e6342ac-ec61-410c-98ba-ae205fb9cc26] F, [2017-09-12T18:43:58.254123 #7393] FATAL -- : [2e6342ac-ec61-410c-98ba-ae205fb9cc26] ActionView::Template::Error (The asset "AdobeStock_95578405.jpeg" is not present in the asset pipeline.): F, [2017-09-12T18:43:58.254377 #7393] FATAL -- : [2e6342ac-ec61-410c-98ba-ae205fb9cc26] 17:
  • [2e6342ac-ec61-410c-98ba-ae205fb9cc26] 18: [2e6342ac-ec61-410c-98ba-ae205fb9cc26] 19: [2e6342ac-ec61-410c-98ba-ae205fb9cc26] 20: {bgposition: "center bottom",bgrepeat: "no-repeat", bgfit: "cover"} , :class => "rev-slidebg" %> [2e6342ac-ec61-410c-98ba-ae205fb9cc26] 21: [2e6342ac-ec61-410c-98ba-ae205fb9cc26] 22: [2e6342ac-ec61-410c-98ba-ae205fb9cc26] 23: <div class="tp-caption dark-translucent-bg" F, [2017-09-12T18:43:58.254415 #7393] FATAL -- : [2e6342ac-ec61-410c-98ba-ae205fb9cc26] F, [2017-09-12T18:43:58.254442 #7393] FATAL -- : [2e6342ac-ec61-410c-98ba-ae205fb9cc26] app/views/creatives/index.html.erb:20:in `_app_views_creatives_index_html_erb___200307797594027740_70304554688000'
  • 更新2

    这是我的资产文件夹结构的图像:

    资产路径

    我还注意到有另一个资产文件夹,因为它预编译了rs-plugin的结构而不是其他文件夹看到图像:

    RS-插件

    这是我的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 Cnd class Application < Rails::Application # Initialize configuration defaults for originally generated Rails version. config.load_defaults 5.1 config.assets.paths << Rails.root.join("app", "assets", "fonts") config.assets.paths << Rails.root.join("app", "assets", "images") config.assets.paths << Rails.root.join("app", "assets", "bootstrap") config.assets.paths << Rails.root.join("app", "assets", "rs-plugin-5") config.assets.paths << Rails.root.join("app", "assets", "magnific-popup") config.assets.paths << Rails.root.join("app", "assets", "owlcarousel2") config.assets.paths << Rails.root.join("app", "assets", "morphext") # 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. end end 

     Rails.application.config.assets.precompile += %w(creative/manifest.js creative/manifest.css images/* bootstrap/* fonts/* magnific-popup/* morphext/* owlcarousel2/* rs-plugin-5/* stylesheets/* javascripts/*) 

    但正如我从你的截图中看到的那样,你的public/assets目录中有一个名为assets的文件夹。

    您的指纹文件预编译文件位于public/assets/assets而不是public/assets

    这意味着如果这些文件正在推向生产(检查它们是否在你的git存储库中,如果没有进行git add ),你将无法在路径localhost:3000/assets/看到它们。 localhost:3000/assets/因为您的目录结构已更改,也许您可​​以在localhost:3000/assets/assets/显示它们,但如果我是您,我将保留正常的目录结构。

    预编译资产意味着创建它的指纹版本

    通过说我想预编译我的所有资产,你说你想要在资产管道中添加或编辑的所有文件生成一个预编译的指纹版本,以便在生产中使用。

    预编译如何工作? 您可以阅读Asset Pipeline Documentation的第4.1章 ,基本上sprockets使用名为.sprockets-manifest-fingerprint.json的文件来检查哪个是生产中使用的指纹文件的最新版本。 这就是有时删除public/assets文件夹解决问题的原因,因为您删除了所有旧文件,包括可能无法使用最新指纹更新的sprockets-manifest.json

    如果在您的public/assets文件夹中找不到它们,那么开发将回退到您的app/assets文件夹中的文件,这就是您只在生产中收到错误的原因。 您可以阅读了解如何禁用此行为

    第3.1和3.2章

    这些步骤也是如此,因为错误通常在开发中更容易解决,因为您不必推送git来查看结果。 有时文件包含在.gitignore或者您需要使用git add app/assets//手动添加该文件

    我们确实在屏幕截图中看到了您的public文件夹,但是我们没有看到您的app/assets文件夹结构和任何sprockets设置,所以可能不是为了获得解决方案而在问题上添加赏金,而只是给我们找到解决方案所需的信息。

    现在这是一个基于意见的通用问题

    另外我相信您通过执行config.assets.prefix = "/some_other_path" 更改了/assets路径

    如果您希望使用rails应用程序,首先要确保这一点。

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

    关于资产编译,请注意,如果您分别在讨论css或js,则可能需要添加到application.css或.js中。 检查一下 。

    例如(如上面的链接)

     app/assets/javascripts/home.js lib/assets/javascripts/moovinator.js vendor/assets/javascripts/slider.js vendor/assets/somepackage/phonebox.js 

    将在这样的清单中引用:

     //= require home //= require moovinator //= require slider //= require phonebox 

    也可以访问子目录中的资产。

     app/assets/javascripts/sub/something.js 

    引用为:

     //= require sub/something 

    我注意到的一些事情:

    1. 您有另一个资产文件夹中的资产文件夹。 这根本不对。
    2. 你能告诉我你的资产目录,你有AdobeStock_95578405.jpeg图片吗?
    3. 在config / initializers / assets.rb文件中添加Rails.application.config.assets.precompile
    4. 哦,顺便说一句,确保每次进行更改时都重新启动服务器。 我已经花了太多时间,因为我完全忘记了这个简单的事情。

    这可能是错误的,但看起来你从预编译目录列表中遗漏了config 。 尝试将列表更新为:

     Rails.application.config.assets.precompile += %w(creative/manifest.js creative/manifest.css images/* bootstrap/* fonts/* magnific-popup/* morphext/* owlcarousel2/* rs-plugin-5/* stylesheets/* javascripts/* config/*)