railsでjavascriptを動かしたい場合どこに記載するのでしょうか?
全ページに共通に、動かしたいです。
application.html.erbには
<%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
application.cssには
/* * This is a manifest file that'll be compiled into application.css, which will include all the files * listed below. * * Any CSS and SCSS file within this directory, lib/assets/stylesheets, or any plugin's * vendor/assets/stylesheets directory can be referenced here using a relative path. * * You're free to add application-wide styles to this file and they'll appear at the bottom of the * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS * files in this directory. Styles in this file should be added after the last require_* statement. * It is generally better to create a new file per style scope. * *= require_tree . *= require_self */
application.js
!function(a,b){"use strict";"object"==typeof module&&"object"==typeof module.exports?module.exports=a.document?以降も続きます。
有名なjqueryを読み込むらしいです。
その下に動かしたいコードを打ってます。
railsではなくマークアップのほうで動作確認はしてます。その動いたコードをコピペしているだけですので、タイプミスはありません。
/*! jQuery v3.1.0 | (c) jQuery Foundation | jquery.org/license */ !function(a,b){"use strict";"object"==typeof module&&"object"==typeof module.exports?module.exports=a.document? (function($) { $(function() { 内容 })(jQuery);
共通に利かせたいので、application.jsに記載したのですが反応がありませんでした。
ご助言よろしくお願いします。
あなたの回答
tips
プレビュー