Ruby jQueryが動作していない場合の対処方法 | Ruby: How to fix method when doesn't work jQuery.
Rubyで、下記のように、javascript_include_tag を使用しても jQueryが機能しなかったので、色々調べてみたら、どうやらこれだけでは jQueryに必要なソースファイルが読み込まれていない模様。
Layout html.erb
<!DOCTYPE html>
<html>
<head>
<title>Cards</title>
<% csrf_meta_tags %>
<%= stylesheet_link_tag 'ajax', 'data-turbolinks-track': 'reload' %>
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
</head>
<body>
<div class="card">
<%= yield %>
</div>
</body>
</html>
<html>
<head>
<title>Cards</title>
<% csrf_meta_tags %>
<%= stylesheet_link_tag 'ajax', 'data-turbolinks-track': 'reload' %>
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
</head>
<body>
<div class="card">
<%= yield %>
</div>
</body>
</html>
下記を追加することで、解決した。
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"><script>
結果、下記のようになった。
<!DOCTYPE html>
<html>
<head>
<title>Cards</title>
<% csrf_meta_tags %>
<%= stylesheet_link_tag 'ajax', 'data-turbolinks-track': 'reload' %>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
</head>
<body>
<div class="card">
<%= yield %>
</div>
</body>
</html>
jQueryが動作しているかどうかを確認する為の html.erbファイルが下記
<span id="ajax">jQuery dosn't work!</span>
<script>
$('#ajax').text('jQuery Running!')
</script>
<script>
$('#ajax').text('jQuery Running!')
</script>
Really annoy ruby....
Really annoy ruby....
Really annoy ruby....