LoginSignup
1
0

More than 3 years have passed since last update.

【Rails】 jQueryが動作しない時の対処法 (読み込む順番に気をつけないといけない)

Last updated at Posted at 2019-04-17

いつも忘れてしまうので備忘録。

ダメなパターン

_head.html.erb
<%= stylesheet_link_tag    'style', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
<%= javascript_include_tag 'selectordie' %>

<!-- JQuery -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>

javascript_include_tagjQuery CDN の順番では期待通りになりませんでした。

正のパターン

_head.html.erb
<!-- JQuery -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>

<%= stylesheet_link_tag    'style', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
<%= javascript_include_tag 'selectordie' %>

jQuery CDNjavascript_include_tag の順番だと期待値通りの動作!

まとめ

先にjQueryロードしないとスクリプト読めないって話。(Railsに限った話じゃない・・・)

1
0
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
1
0