0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[Rails] jQueryの導入

Last updated at Posted at 2021-09-16

以下、Ruby on Rails 6.0.0における
jQueryのインストール方法です。

  1. gemの導入
  2. application.jsに記述追加
  3. application.html.erbのhead部に記述追加
  4. 任意の内容で動作確認

#1. gemの導入

以下の記述を追加し、bundle installを打ちます。

Gemfile
gem 'jquery-rails'

#2. application.jsに記述追加

ディレクトリがない場合は
apps/assets/javascripts/application.js
作成します。

app/assets/javascripts/application.js
//= require jquery
//= require jquery_ujs

#3. application.html.erbのhead部に記述追加
以下の一行を追加します。

views/layouts/application.html.erb

<head>

#(中略)

<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>

</head>

#4. 任意の内容で動作確認

~.html.erb

<p>jQuery適用前</p>
<script type="text/javascript">
  $(document).ready(function(){
    $("p").text("jQuery適用後");
  });
</script>

表示内容が
"jQuery適用後"
として表示されていれば成功です。

今回は以上です。

お読みくださりありがとうございました。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?