【背景】
現在webAPIを使ったrailsのwebアプリケーションの作成に挑んでいるのですが、jsの読み込みがうまくいかず手間取っています。
【現在の状況】
コントローラーごとにlayoutを変更しようと思い、大元のlayoutと現在作成しているwebAPIを使用したコントローラーでは分けています。
大元のlayout application.html.erb
<head> <title>ProjectNobu</title> <%= csrf_meta_tags %> <%= csp_meta_tag %> <%= stylesheet_link_tag 'application', media: 'all', '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' %> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> </head>
webAPIを使用としているコントローラー fortune_cookie.html.erb
<head> <title>Fortune_Cookie</title> <%= csrf_meta_tags %> <%= csp_meta_tag %> <%= stylesheet_link_tag 'application', media: 'all', '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' %> <!-- モバイル適用 --> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> </head>
'assets内のapplication.js'
// This is a manifest file that'll be compiled into application.js, which will include all the files // listed below. // // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, or any plugin's // vendor/assets/javascripts directory can be referenced here using a relative path. // // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the // compiled file. JavaScript code in this file should be added after the last require_* statement. // // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details // about supported directives. // //= require rails-ujs //= require activestorage //= require fortune_api //= require profile // Bootstarpと依存関係にある //= require jquery3 //= require popper //= require bootstrap
jqueryが動くかどうかのjsファイル
$('#button').on('click', function() { console.log("どうもどうも"); });
直接html部分にscriptタグを埋め込むと動作ができるのですが、assets内のjavascriptファイルに入れるような形で動作できるようにしたいです。
お分かりになる方がいましたら、どうぞよろしくお願いいたします。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。