【问题标题】:Rails 5 Assets not compiling images or cssRails 5资产不编译图像或css
【发布时间】:2020-10-10 17:33:49
【问题描述】:

如果有任何帮助,我将不胜感激。我最近将我的应用程序更新到 rails 5.2.3(从 4.0.1 开始),并注意到当我向资产/图像添加新图像时,它无法在生产中显示。我最终运行了 rake assets:clobber 并尝试使用 rake assets:precompile 重建我的资产。不幸的是,现在我所有的资产都死了。

当我启动 Rails 服务器并转到 localhost:3000 时,开发中的一切都按预期工作

我尝试修改 production.rb 配置文件,但没有取得任何进展。任何帮助将不胜感激,我在这个网站上做了很多工作,它用于世界各地的医疗专业人员。无论如何,我都不是专业的程序员。我注意到运行后

assets:precompile RAILS_ENV=production

创建了一个新的 public/assets 文件夹,但只包含三个文件:

  • .sprockets-manifest-.....json
  • 清单-....js
  • 清单-.....js.gz

而不是我的资产/图像文件夹中的所有图像

production.rb

BoardsApp::Application.configure do
  # Settings specified here will take precedence over those in config/application.rb.

  # Code is not reloaded between requests.
  config.cache_classes = true

  # Eager load code on boot. This eager loads most of Rails and
  # your application in memory, allowing both thread web servers
  # and those relying on copy on write to perform better.
  # Rake tasks automatically ignore this option for performance.
  config.eager_load = true

  # Full error reports are disabled and caching is turned on.
  config.consider_all_requests_local       = false
  config.action_controller.perform_caching = true

  # Enable Rack::Cache to put a simple HTTP cache in front of your application
  # Add `rack-cache` to your Gemfile before enabling this.
  # For large-scale production use, consider using a caching reverse proxy like nginx, varnish or squid.
  # config.action_dispatch.rack_cache = true

  # Disable Rails's static asset server (Apache or nginx will already do this).
  config.serve_static_assets = false

  # Compress JavaScripts and CSS.
  config.assets.js_compressor = :uglifier
  # config.assets.css_compressor = :sass

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

  # Generate digests for assets URLs.
  config.assets.digest = true

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

  # Specifies the header that your server uses for sending files.
  # config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache
  # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx

  # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
  # config.force_ssl = true

  # Set to :debug to see everything in the log.
  config.log_level = :info

  # Prepend all log lines with the following tags.
  # config.log_tags = [ :subdomain, :uuid ]

  # Use a different logger for distributed setups.
  # config.logger = ActiveSupport::TaggedLogging.new(SyslogLogger.new)

  # Use a different cache store in production.
  # config.cache_store = :mem_cache_store

  # Enable serving of images, stylesheets, and JavaScripts from an asset server.
  # config.action_controller.asset_host = "http://assets.example.com"

  # Precompile additional assets.
  # application.js, application.css, and all non-JS/CSS in app/assets folder are already added.
  # config.assets.precompile += %w( search.js )

  # Ignore bad email addresses and do not raise email delivery errors.
  # Set this to true and configure the email server for immediate delivery to raise delivery errors.
  # config.action_mailer.raise_delivery_errors = false

  # Enable locale fallbacks for I18n (makes lookups for any locale fall back to
  # the I18n.default_locale when a translation can not be found).
  config.i18n.fallbacks = true

  # Send deprecation notices to registered listeners.
  config.active_support.deprecation = :notify

  # Disable automatic flushing of the log to improve performance.
  # config.autoflush_log = false

  # Use default logging formatter so that PID and timestamp are not suppressed.
  config.log_formatter = ::Logger::Formatter.new
end

宝石文件

source 'https://rubygems.org'
ruby '2.6.3'
gem 'rails', '5.2.3'
gem 'puma'
gem 'bootstrap-sass'
gem 'bootstrap-switch-rails'
gem 'nokogiri'
gem 'pg', '0.21.0'
gem 'haml'
gem 'friendly_id'
gem 'rake'

group :development, :test do
  gem 'rspec-rails'
end

group :test do
  gem 'selenium-webdriver'
  gem 'capybara'
end
  
gem 'sass-rails'
gem 'uglifier'
gem 'coffee-rails'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder'

group :doc do
  gem 'sdoc', require: false
end

group :production do
  gem 'rails_12factor', '0.0.2'
end

更新 我从 Heroku 上的早期部署中恢复了网站,所以至少现在它还活着,但我还没有解决如何更新的问题,因为我的图像似乎都没有被编译。

当我在生产环境中运行 rails server 时,我得到了这个错误:

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

而在开发中,一切都按预期加载。 更新 已解决! 感谢您建议查看 heroku 日志。我发现了错误:

Asset `application.css` was not declared to be precompiled in production

从那里我找到了https://github.com/rails/sprockets-rails/issues/458这个讨论。

我将 sprockets-rails gem 降级到 2.3.3。并运行 rake assets:precompile ,一切正常。

【问题讨论】:

  • 在这样做之后,应用程序完全崩溃了,我得到了我们很抱歉,但出现了问题页面。
  • 当您遇到出现问题的页面时,您应该查看日志文件以找出究竟出了什么问题,然后您可以着手修复它,所以发布您得到的实际错误跨度>
  • assets:clobber 将清空您的资产文件夹。您将需要重新编写所有 css 和 javascript 并重新复制到您的图像中,然后预编译资产。因此,从旧项目文件夹中复制所有资产并尝试再次编译。每次添加资产并发布到生产环境时,都必须编译
  • app/assets 文件夹仍然像往常一样包含我所有的图像和 css.scss 文件和 js.coffee 文件。该应用程序在开发中完美运行。在我升级到 Rails 5 之前,生产中出现了一些从未发生过的问题。由于某种原因,rake assets:precompile 似乎无法编译。
  • 您在编译资产时看到了什么错误或错误?

标签: ruby-on-rails ruby-on-rails-5 assets


【解决方案1】:

我在 Ubuntu 20.04 中处理 Rails 6 应用程序时遇到了同样的问题。

对我来说,问题是我没有在生产环境中正确地提供预编译资产。

这是我修复它的方法

首先,我运行以下命令来预编译资产并使其在我的应用程序的public 目录中可用:

rails assets:precompile RAILS_ENV=production

这会将图像、css和js从app/assetsapp/javascript编译并复制到public/

注意:在开发中,这可以使用webpacker 和命令:/bin/webpack-dev-server 来完成

接下来,设置 NginxApache 网络服务器以提供在我的应用程序的 public 目录中可用的静态文件。对我来说,我使用 Let's Encrypt for SSL 使用以下配置设置 Nginx

upstream railsserver {
        server 127.0.0.1:3000;
}

server {

        # Replace 'localhost' with your FQDN if you want to use
        # your app from remote or if you need to add a certificate 
        server_name my-website.com www.my-website.com;
                        
        root /home/deploy/my-website/public;

        # Define where Nginx should write its logs
        access_log /var/log/nginx/my-website/access.log;
        error_log /var/log/nginx/my-website/error.log;

        location / {
            # First attempt to serve request as file, then
            # the rails application directory
            try_files $uri @railsserver;
        }
       
        location ~ ^/(assets/|robots.txt|humans.txt|favicon.ico) {
                expires max;
        }

        location @railsserver {
                proxy_set_header Host $http_host;
                proxy_set_header CLIENT_IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto $scheme;
                proxy_read_timeout 300;
                proxy_pass http://railsserver;

                gzip on;
                gzip_types text/plain text/xml text/css image/svg+xml application/javas$
                gzip_proxied any;
        }

        listen [::]:443 ssl ipv6only=on; # managed by Certbot
        listen 443 ssl; # managed by Certbot
        ssl_certificate /etc/letsencrypt/live/my-website.com/fullchain.pem; # managed by Certbot
        ssl_certificate_key /etc/letsencrypt/live/my-website.com/privkey.pem; # managed by Certbot
        include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
        ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

就是这样。

我希望这会有所帮助

【讨论】:

    猜你喜欢
    • 2017-07-07
    • 1970-01-01
    • 2013-12-25
    • 2018-07-04
    • 1970-01-01
    • 2014-07-24
    • 1970-01-01
    • 2011-11-30
    • 1970-01-01
    相关资源
    最近更新 更多