LoginSignup
5
4

More than 3 years have passed since last update.

[Rails]EC2にデプロイ後、jQuery/JavaScriptが動かない

Posted at

前提

Rails 6.0.3.1
ruby 2.6.3
unicorn-5.5.5
nginx/1.16.1

 EC2にデプロイしたら、jQueryがお仕事をサボっていた

デプロイ後に、ブラウザでトップページを確認すると jQueryが動いておらず、トップページのfade inがいつまでたってもなされない状態でした。

結論

app/views/layouts/_default.html.erb
<%= csrf_meta_tags %>
<%= csp_meta_tag %>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
<%= stylesheet_pack_tag 'application', 'data-turbolinks-track': 'reload' %>

#以下を追加
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

ローカルでは、

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

なしでも動いていましたが、どうやら本番環境では追加しないとうまく動かない場合があるみたいです。

ちなみに
httpsではなく、httpでも動くようですが、SSL化したアプリケーションでは大体のアプリケーションは、セキュリティの面でSSL化すると思うので、httpsでしてあげた方が良さそうです。

 > railsでjQuery/JavaScriptがうまく動かない時の対処法

5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4