前提・実現したいこと
アプリをデプロイしたらブラウザでWe're sorry, but something went wrong.というエラーが出ました。
ログを確認したところ
ActionView::Template::Error (The asset "application.css" is not present in the asset pipeline.
というエラーが出ていました。
発生している問題・エラーメッセージ
[d86058c4-3e19-4839-8911-fe7410f859ec] app/views/layouts/application.html.erb:10 [983873fe-9610-4044-89f0-7fa37184c9f1] ActionView::Template::Error (The asset "application.css" is not present in the asset pipeline. ):
該当のソースコード
#####assets/stylesheets/application.scss
省略 * It is generally better to create a new file per style scope. * */ @import "articles"; @import "bootstrap-sprockets"; @import "bootstrap";
#####config/environment/production.rb
Rails.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 threaded 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 # Ensures that a master key has been made available in either ENV["RAILS_MASTER_KEY"] # or in config/master.key. This key is used to decrypt credentials (and other encrypted files). # config.require_master_key = true # 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? # Compress CSS using a preprocessor. # config.assets.css_compressor = :sass # Do not fallback to assets pipeline if a precompiled asset is missed. config.assets.compile = ture # Enable serving of images, stylesheets, and JavaScripts from an asset server. # config.action_controller.asset_host = 'http://assets.example.com' # 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 # Store uploaded files on the local file system (see config/storage.yml for options). config.active_storage.service = :local # Mount Action Cable outside main process or domain. # config.action_cable.mount_path = nil # config.action_cable.url = 'wss://example.com/cable' # config.action_cable.allowed_request_origins = [ 'http://example.com', /http://example.*/ ] # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies. # config.force_ssl = true # Use the lowest log level to ensure availability of diagnostic information # when problems arise. config.log_level = :debug # Prepend all log lines with the following tags. config.log_tags = [ :request_id ] # Use a different cache store in production. # config.cache_store = :mem_cache_store # Use a real queuing backend for Active Job (and separate queues per environment). # config.active_job.queue_adapter = :resque # config.active_job.queue_name_prefix = "stock_info_production" config.action_mailer.perform_caching = false # 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 cannot be found). config.i18n.fallbacks = true # Send deprecation notices to registered listeners. config.active_support.deprecation = :notify # Use default logging formatter so that PID and timestamp are not suppressed. config.log_formatter = ::Logger::Formatter.new # Use a different logger for distributed setups. # require 'syslog/logger' # config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new 'app-name') if ENV["RAILS_LOG_TO_STDOUT"].present? logger = ActiveSupport::Logger.new(STDOUT) logger.formatter = config.log_formatter config.logger = ActiveSupport::TaggedLogging.new(logger) end # Do not dump schema after migrations. config.active_record.dump_schema_after_migration = false # Inserts middleware to perform automatic connection switching. # The `database_selector` hash is used to pass options to the DatabaseSelector # middleware. The `delay` is used to determine how long to wait after a write # to send a subsequent read to the primary. # # The `database_resolver` class is used by the middleware to determine which # database is appropriate to use based on the time delay. # # The `database_resolver_context` class is used by the middleware to set # timestamps for the last write to the primary. The resolver uses the context # class timestamps to determine how long to wait before reading from the # replica. # # By default Rails will store a last write timestamp in the session. The # DatabaseSelector middleware is designed as such you can define your own # strategy for connection switching and pass that into the middleware through # these configuration options. # config.active_record.database_selector = { delay: 2.seconds } # config.active_record.database_resolver = ActiveRecord::Middleware::DatabaseSelector::Resolver # config.active_record.database_resolver_context = ActiveRecord::Middleware::DatabaseSelector::Resolver::Session end
試したこと1
参考記事で,config/environment/production.rbファイルにconfig.assets.js_compressor = :uglifierという記述をconfig.assets.js_compressor = Uglifier.new(harmony: true)に編集する,と書いてありましたが、自分のファイルにはその元々その記述がなかったため、config.assets.js_compressor = Uglifier.new(harmony: true)を新たに追記してみましたが、上手くいきませんでした。
試したこと2
config/environment/production.rbファイルのconfig.assets.compile = falseをtrueにしました。
###試したこと3
config/environment/production.rbファイルの
config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?を
config.public_file_server.enabled = trueに編集しました。
試したこと4
参考記事ではbundle exec rails assets:precompile RAILS_ENV=productionで手動デプロイをしたあと
terminal
1[username@ip-10-0-11-63 app]$ cd public/assets/ 2[username@ip-10-0-11-63 assets]$ ls 3application-111ac35321e379f53529ace176fe931c952be73f7ffd688b815ec21878126685.js 4application-111ac35321e379f53529ace176fe931c952be73f7ffd688b815ec21878126685.js.gz 5application-e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855.css 6application-e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855.css.gz
となっていました。
しかし自分の方ではbundle exec rails assets:precompile RAILS_ENV=productionを実行した後、assetsディレクトリが存在しませんでした。
terminal
1[username@ip-10-0-11-63 app]$ cd public 2[username@ip-10-0-11-63 public]$ ls 3404.html 422.html 500.html apple-touch-icon.png apple-touch-icon-precomposed.png favicon.ico robots.txt
試したこと5
unicornとnginxを切断した上でコンパイルしました。
unicornやnginxの再起動は何回もしています。
補足情報(FW/ツールのバージョンなど)
rails 6.0.3.5
ruby 2.6.5
mysqld 5.7.33
アプリの方で見た目を整える際に、scssファイルとbootstrap両方使ってます。
どちらかに統一しなければいけないというエラーなのでしょうか...
2日ほどこのエラーと戦っているのですが、設定ファイルをいじったりunicornやnginxを再起動しまくっているうちに、何が何だかわからなくなってしまいました。
解決方法をご存知の方がいらっしゃれば、ぜひご教授していただけると幸いです
####追記 試したこと6
シークレットキーあたりが怪しいと思い、
ローカル環境で一度credentials.yml.encファイルを削除し、作り直しました。
rm credentials.yml.enc
rails credentials:edit
その後、ec2内の方のアプリのcredentials.yml.encファイルに、ローカル環境で作り直したcredentials.yml.encをコピーしました。
それからbundle exec rails assets:precompile RAILS_ENV=productionを実行すると、
ActiveSupport::MessageEncryptor::InvalidMessage: ActiveSupport::MessageEncryptor::InvalidMessage
というエラーが出ました。
ここに原因がありそうと思っているのですが...話をややこしくしてしまったのか...
###追記
#####app/assets/stylesheets/application.scss を css に書き換えてRAILS_ENV=production bundle exec rails assets:precompile実行
#####app/assets/config/manifest.js
//= link_tree ../images //= link_directory ../stylesheets .css
#####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 # Add Yarn node_modules folder to the asset load path. Rails.application.config.assets.paths << Rails.root.join('node_modules') # Precompile additional assets. # application.js, application.css, and all non-JS/CSS in the app/assets # folder are already added. # Rails.application.config.assets.precompile += %w( admin.js admin.css )
あなたの回答
tips
プレビュー