LoginSignup
1
3

More than 3 years have passed since last update.

Rails6 Ajaxを使えるようにするために最初にしておくこと

Last updated at Posted at 2021-01-10

ポートフォリを作成中にAjaxで路頭に迷ったので、未来の自分のためにこの記事を残しておこうと思います。

jQuery関係

jQueryのライブラリをインストール

$ yarn add jquery@3.4.1

Webpackの環境編集

config/webpack/environment.js
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

application.jsに追加

app/javascript/packs/application.js
require("jquery")

ブラウザ関係

ブラウザ側でjavascriptが無効になっていた場合でもAjaxが起動するために

config/application.rb
module "アプリ名"
  class Application < Rails::Application
    .
    .
    .
    # これを追加!
    config.action_view.embed_authenticity_token_in_remote_forms = true
  end

あとは書きまくるだけ!!

1
3
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
1
3