質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby on Rails 6

Ruby on Rails 6は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Amazon EC2

Amazon EC2は“Amazon Elastic Compute Cloud”の略称です。Amazon Web Services(AWS)の一部であり、仮想化されたWebサーバーのコンピュータリソースをレンタルできるサービスです。

Q&A

解決済

2回答

3999閲覧

【rails6】本番環境でJSが読み込まれない

kickthekaz

総合スコア29

Ruby on Rails 6

Ruby on Rails 6は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Amazon EC2

Amazon EC2は“Amazon Elastic Compute Cloud”の略称です。Amazon Web Services(AWS)の一部であり、仮想化されたWebサーバーのコンピュータリソースをレンタルできるサービスです。

0グッド

1クリップ

投稿2020/06/03 13:35

編集2020/06/04 10:14

表題の件ですが、開発環境ではJSが問題なく読み込まれるのですが、
本番環境になると、読み込まれません。。

内容)
本番環境のブラウザコンソールで以下のエラーが出ております。

http://〇〇/packs/js/application-f526a99e9d48b79e794e.js net::ERR_ABORTED 404 (Not Found)

cssは問題なく読み込まれております。

viewのapplication.slimは以下内容です。

slim

1doctype html 2html 3 head 4 = display_meta_tags(default_meta_tags) 5 meta http-equiv="X-UA-Compatible" content="IE=edge" 6 meta name="viewport" content="width=device-width, initial-scale=1" 7 = csrf_meta_tags 8 = csp_meta_tag 9 = stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' 10 = javascript_pack_tag 'application', 'data-turbolinks-track': 'reload'

config/environments/production.rb

rb

1Rails.application.configure do 2 # Settings specified here will take precedence over those in config/application.rb. 3 4 # Code is not reloaded between requests. 5 config.cache_classes = true 6 7 # Eager load code on boot. This eager loads most of Rails and 8 # your application in memory, allowing both threaded web servers 9 # and those relying on copy on write to perform better. 10 # Rake tasks automatically ignore this option for performance. 11 config.eager_load = true 12 13 # Full error reports are disabled and caching is turned on. 14 config.consider_all_requests_local = false 15 config.action_controller.perform_caching = true 16 17 # Ensures that a master key has been made available in either ENV["RAILS_MASTER_KEY"] 18 # or in config/master.key. This key is used to decrypt credentials (and other encrypted files). 19 # config.require_master_key = true 20 21 # Disable serving static files from the `/public` folder by default since 22 # Apache or NGINX already handles this. 23 config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present? 24 25 # Compress CSS using a preprocessor. 26 # config.assets.css_compressor = :sass 27 28 # Do not fallback to assets pipeline if a precompiled asset is missed. 29 config.assets.compile = false 30 31 # Enable serving of images, stylesheets, and JavaScripts from an asset server. 32 # config.action_controller.asset_host = 'http://assets.example.com' 33 34 # Specifies the header that your server uses for sending files. 35 # config.action_dispatch.x_sendfile_header = 'X-Sendfile' # for Apache 36 # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for NGINX 37 38 # Store uploaded files on the local file system (see config/storage.yml for options). 39 config.active_storage.service = :local 40 41 # Mount Action Cable outside main process or domain. 42 # config.action_cable.mount_path = nil 43 # config.action_cable.url = 'wss://example.com/cable' 44 # config.action_cable.allowed_request_origins = [ 'http://example.com', /http://example.*/ ] 45 46 # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies. 47 # config.force_ssl = true 48 49 # Use the lowest log level to ensure availability of diagnostic information 50 # when problems arise. 51 config.log_level = :debug 52 53 # Prepend all log lines with the following tags. 54 config.log_tags = [ :request_id ] 55 56 # Use a different cache store in production. 57 # config.cache_store = :mem_cache_store 58 59 # Use a real queuing backend for Active Job (and separate queues per environment). 60 # config.active_job.queue_adapter = :resque 61 # config.active_job.queue_name_prefix = "creditcard_com_production" 62 63 config.action_mailer.perform_caching = false 64 65 config.action_mailer.raise_delivery_errors = true 66 config.action_mailer.delivery_method = :smtp 67 config.action_mailer.smtp_settings = { 68 port: 587, 69 address: 'smtp.gmail.com', 70 domain: 'gmail.com', 71 authentication: 'plain', 72 enable_starttls_auto: true 73 } 74 75 # Ignore bad email addresses and do not raise email delivery errors. 76 # Set this to true and configure the email server for immediate delivery to raise delivery errors. 77 # config.action_mailer.raise_delivery_errors = false 78 79 # Enable locale fallbacks for I18n (makes lookups for any locale fall back to 80 # the I18n.default_locale when a translation cannot be found). 81 config.i18n.fallbacks = true 82 83 # Send deprecation notices to registered listeners. 84 config.active_support.deprecation = :notify 85 86 # Use default logging formatter so that PID and timestamp are not suppressed. 87 config.log_formatter = ::Logger::Formatter.new 88 89 # Use a different logger for distributed setups. 90 # require 'syslog/logger' 91 # config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new 'app-name') 92 93 if ENV["RAILS_LOG_TO_STDOUT"].present? 94 logger = ActiveSupport::Logger.new(STDOUT) 95 logger.formatter = config.log_formatter 96 config.logger = ActiveSupport::TaggedLogging.new(logger) 97 end 98 99 # Do not dump schema after migrations. 100 config.active_record.dump_schema_after_migration = false 101 102 # Inserts middleware to perform automatic connection switching. 103 # The `database_selector` hash is used to pass options to the DatabaseSelector 104 # middleware. The `delay` is used to determine how long to wait after a write 105 # to send a subsequent read to the primary. 106 # 107 # The `database_resolver` class is used by the middleware to determine which 108 # database is appropriate to use based on the time delay. 109 # 110 # The `database_resolver_context` class is used by the middleware to set 111 # timestamps for the last write to the primary. The resolver uses the context 112 # class timestamps to determine how long to wait before reading from the 113 # replica. 114 # 115 # By default Rails will store a last write timestamp in the session. The 116 # DatabaseSelector middleware is designed as such you can define your own 117 # strategy for connection switching and pass that into the middleware through 118 # these configuration options. 119 # config.active_record.database_selector = { delay: 2.seconds } 120 # config.active_record.database_resolver = ActiveRecord::Middleware::DatabaseSelector::Resolver 121 # config.active_record.database_resolver_context = ActiveRecord::Middleware::DatabaseSelector::Resolver::Session 122end 123

config/webpacker.yml

# Note: You must restart bin/webpack-dev-server for changes to take effect default: &default source_path: app/javascript source_entry_path: packs public_root_path: public public_output_path: packs cache_path: tmp/cache/webpacker check_yarn_integrity: false webpack_compile_output: true # Additional paths webpack should lookup modules # ['app/assets', 'engine/foo/app/assets'] resolved_paths: [] # Reload manifest.json on all requests so we reload latest compiled packs cache_manifest: false # Extract and emit a css file extract_css: false static_assets_extensions: - .jpg - .jpeg - .png - .gif - .tiff - .ico - .svg - .eot - .otf - .ttf - .woff - .woff2 extensions: - .mjs - .js - .sass - .scss - .css - .module.sass - .module.scss - .module.css - .png - .svg - .gif - .jpeg - .jpg development: <<: *default compile: true # Verifies that correct packages and versions are installed by inspecting package.json, yarn.lock, and node_modules check_yarn_integrity: true # Reference: https://webpack.js.org/configuration/dev-server/ dev_server: https: false host: localhost port: 3035 public: localhost:3035 hmr: false # Inline should be set to true if using HMR inline: true overlay: true compress: true disable_host_check: true use_local_ip: false quiet: false pretty: false headers: 'Access-Control-Allow-Origin': '*' watch_options: ignored: '**/node_modules/**' test: <<: *default compile: true # Compile test packs to a separate directory public_output_path: packs-test production: <<: *default # Production depends on precompilation of packs prior to booting for performance. compile: false # Extract and emit a css file extract_css: true # Cache manifest.json for performance cache_manifest: true

デプロイは、capiを利用しております。

もし、お分かりになればご教授いただけませんでしょうか。
よろしくお願いします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

自己解決

winterboumさん
ありがとうございました。。

結局自己解決できました。

結論、nginxの設定にミスがありました。。

root /var/www/<アプリケーション名>/public; ではなく、 root /var/www/<アプリケーション名>/current/public;

capiを導入したので、変更漏れでした。
イージーミスでした。
すみません。
丸一日以上悩みました。。

以上です。

投稿2020/06/04 10:54

kickthekaz

総合スコア29

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

assets:precompile はやってありますか?

投稿2020/06/03 22:52

winterboum

総合スコア23329

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

kickthekaz

2020/06/04 01:03

ご質問ありがとうございます。 bundle exec cap production deployでデプロイしているので、その際、実行しております!
winterboum

2020/06/04 01:11

application-f526a99e9d48b79e794e.js これ短い気がします。 application-c759687c7a3278e97a6a0683d2d1b39a1007e4d7209e6af5a5da3e222c02caf1.js このくらいあるはず。 また /packs/js/ も???"/assets/application- 。。。。 だとおもうのですが = javascript_pack_tag がどう変換されてるか htmlソースを確認。 public/assets があるか、そこに application*.JSがあるか確認 をしてみてください
kickthekaz

2020/06/04 06:45

= javascript_pack_tagですが、以下のように変換されています。 <script src="/packs/js/application-f526a99e9d48b79e794e.js" data-turbolinks-track="reload"></script> ec2のcurrentでは、public/packs/jsにapplication-f526a99e9d48b79e794e.jsがあるが確認できたのですが、読み取れない状況です。。 ブラウザコンソールで以下が発生します。。 Failed to load resource: the server responded with a status of 404 (Not Found)
winterboum

2020/06/04 07:45

rails6か。6だと縮めてるのかな。 さて、なぜだ。。。 dirとfileのpermittiionはどうなってますか? webサーバーの起動ユーザーが読める様になってますか
kickthekaz

2020/06/04 10:06

全て確認できるようになっています。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問