LoginSignup
1
2

More than 5 years have passed since last update.

rails5でjQueryを使う

Posted at

rails5でjQueryを使う方法

rails5でjQueryを使うためには、

1.gemのインストール,
2.application.jsへの記述 
3.application.html.erbへの記述

の3つの作業が必要

1.gemのインストール

gemfileに gem "jquery-rails"を記述し、bundle install

gemfile
gem "jquery-rails"

2.application.jsファイルへの記述

app/assets/javasciript下にある、application.jsにjQueryを読み込ませるコードをかく
(必ずapplication.jsにかくこと。ほかのjsファイルではダメ!)

application.js
//= require jquery
//= require jquery_ujs

3.application.html.erb

application.html.erbのhead内に以下の記述をする(すでに書いてあったらそのままにする)
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>

最後に

Qiita初投稿です。よろしくお願いします。

1
2
2

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
2