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.

Rails6でajaxを使う jQuery3.0

Last updated at Posted at 2021-09-10

rails6のバージョンだとうまく動かなかったので。
元記事
https://qiita.com/moriwm77/items/ea54c0f34da073801bf1

バージョン
ruby-2.7.4
Rails 6.1.4.1
jquery 3.6.0

とりあえずscaffoldを導入

scaffoldについて
https://qiita.com/tsubasaozawa/items/98f3e64a450d4304fc84

文章を投稿するのでカラムにtitleを使います。

rails g scaffold task title:string
rails db:create # データベース作成
rails db:migrate # マイグレーション実行
yarn add jquery

ここからは下記ページのJqueryの設定を行います

config/webpack/environment.js

下記コードをコピペしてconfig/webpack/environment.jsに貼り付ける

const webpack = require('webpack')
environment.plugins.prepend('Provide',
  new webpack.ProvidePlugin({
    $: 'jquery/src/jquery',
    jQuery: 'jquery/src/jquery'
  })
)

下記のように貼る

const { environment } = require('@rails/webpacker')
//ここから
const webpack = require('webpack')
environment.plugins.prepend('Provide',
  new webpack.ProvidePlugin({
    $: 'jquery/src/jquery',
    jQuery: 'jquery/src/jquery'
  })
)
//ここまで
module.exports = environment

次に

app/javascript/packs/application.js

ここへ下記コードを追加する

require("jquery")

あとは

app/javascript直下にjsファイルを作成すればOKです


tasks.jsで
$(document).ready(function() {とあるが
ここは下記に変更する

$(function() {

image.png

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?