[RUBY] The first thing to do before you can use Rails6 Ajax

I got lost in Ajax while creating a portfolio, so I'll leave this article for myself in the future.

jQuery related

Install jQuery library

$ yarn add [email protected]

Webpack environment editing

config/webpack/environment.js


const { environment } = require('@rails/webpacker')

#Add the following
const webpack = require('webpack')
environment.plugins.prepend('Provide',
  new webpack.ProvidePlugin({
    $: 'jquery/src/jquery',
    jQuery: 'jquery/src/jquery'
  })
)
#So far
module.exports = environment

Added to application.js

app/javascript/packs/application.js


require("jquery")

Browser related

In order for Ajax to start even if javascript is disabled on the browser side

config/application.rb


module "app name"
  class Application < Rails::Application
    .
    .
    .
    #Add this!
    config.action_view.embed_authenticity_token_in_remote_forms = true
  end

All you have to do is write it down! !!

Recommended Posts

The first thing to do before you can use Rails6 Ajax
What to do if you can't use the rails command
[PostgreSQL] If you want to delete the Rails app, delete the database first!
The first thing to do when you want to be happy with Heroku on GitHub with Eclipse in Java
[Rails + Webpacker] I want to use images of assets! Until you can view the image in Vue.js
When you want to use the method outside
What do you use when converting to String?
[Java] Why do you bother to use the interface (Spring is also available)
What to do if the Rails server can't start
[Rails] I don't know how to use the model ...
[Rails] What to do if you accidentally install bundle in the production environment in your local environment
[Rails Tutorial Chapter 2] What to do when you make a mistake in the column name
[Rails] What to do if you can't get an error message with the errors method
Why can I use the rails command installed with gem? ??
What to do when you launch an application with rails
You can solve the problem by referring to the two articles !!!
# What to do if you accidentally do rails db: migrate: drop
[Rails] How to use enum
[Rails] How to use enum
How to use rails join
[Rails] How to use validation
[Rails] How to use authenticate_user!
[Rails] How to use "kaminari"
[Rails] How to use Scope
What to do if Cloud9 is full in the Rails tutorial
[Rails] Don't use the select method just to narrow down the columns!
[Ruby on Rails] Use the resources method to automatically create routes.
What to do if you forget the root password in CentOS7
What to do if you cannot roll back the migration (UnknownMigrationVersionError)
[Rails] What to do if you can't get parameters with form_with
Summary of tips (Rails) that you can use all the way because you understand that it is jQuery discount
What to do if you can't bundle update and bundle install after installing Ruby 3.0.0 in the Rails tutorial
[Rails] How to use gem "devise"
[Rails] How to use devise (Note)
How to use the form_with method
[Rails] How to use flash messages
How to use the wrapper class
How to use Ruby on Rails
Until you introduce fonts to Rails
When will you do the refactoring?
[Introduction to Rails] How to use render
Do you use Stream in Java?
What to do if you get an error during rails db: reset
[Rails] Use devise to get information about the currently logged in user
What to do if you don't like the code generated by swagger-codegen-cli
To you who were told "Don't use Stream API" in the field
I want you to use Enum # name () for the Key of SharedPreference
How to embed and display youtube videos in Rails (You can also get the URL you entered by editing)
What to do if you get an "A server is already running." Error when you try to start the rails server
[Rails] Display people who have DMed on My Page so that you can link to the chat page! 【memorandum】
[Rails] I want to reset everything because the data in the local environment is strange! What to do before that