How to deploy jQuery on Rails

Introduction of jQuery

Check gem file

In the Gemfile,

gem 'jquery-rails'

If not, fill it out at the bottom of the file yourself and in the terminal

bundle install

To execute.

Change the contents of application.js

//= require jquery
//= require rails-ujs
//= require activestorage
//= require_tree .

Change as above. It seems that the order is important.

Check if it works

Select a view file appropriately and put it in it
<h1>test</h1>
<script type="text/javascript">
  $("h1").css("color", "red");
</script>

If you write like this, jQuery will work and the characters of the h1 tag will turn red.

Write jQuery in another file

As the code increases, the view file becomes complicated as it is, so create a separate file like test.js. The place to create is under "app/assets/javascripts".

According to the jQuery format

$(function() {
 $("h1").css("color", "green");
});

Write the code like this.

Delete the written in the view file earlier.

Now, check it again in the browser, and if the characters in the h1 tag are red, jQuery has started successfully.

Reference article

https://web-camp.io/magazine/archives/17690 This article was very easy to understand.

Recommended Posts

How to deploy jQuery on Rails
How to deploy Bootstrap on Rails
How to deploy on heroku
How to deploy Laravel on CentOS 7
How to use Ruby on Rails
How to deploy
Rails on Tiles (how to write)
How to introduce jQuery in Rails 6
Deploy Rails on Docker to heroku
How to deploy jQuery in your Rails app using Webpacker
How to deploy a Rails application on AWS (article summary)
[Ruby on Rails] How to use CarrierWave
Deploy to Heroku [Ruby on Rails] Beginner
Preparing to introduce jQuery to Ruby on Rails
[Ruby on Rails] How to use redirect_to
[Ruby on Rails] How to use kaminari
How to write Rails
Deploy RAILS on EC2
How to uninstall Rails
Deploy to Ruby on Rails Elastic beanstalk (EB deploy)
[Ruby on Rails] How to display error messages
How to deploy a container on AWS Lambda
How to add / remove Ruby on Rails columns
How to use JQuery in js.erb of Rails6
[Rails MySQL] How to reset DB on heroku
[Ruby on Rails] How to install Bootstrap in Rails
[Ruby on Rails] How to use session method
[rails] How to post images
[Rails] How to use enum
[Rails] How to install devise
[Rails] How to use enum
How to read rails routes
How to use rails join
How to terminate rails server
How to write Rails validation
How to write Rails seed
[Rails] How to use validation
[Rails] How to disable turbolinks
[Rails] How to use authenticate_user!
[Rails] How to use "kaminari"
[Rails] How to implement scraping
[Rails] How to make seed
How to write Rails routing
[Rails] How to install simple_calendar
[Rails] How to install reCAPTCHA
[Rails] How to use Scope
[Ruby on Rails] How to write enum in Japanese
[Ruby on Rails] How to change the column name
Deploy to Ruby on Rails Elastic beanstalk (Environment construction)
[Ruby On Rails] How to reset DB in Heroku
(Ruby on Rails6) How to create models and tables
[Rails] How to install Font Awesome
[Rails] How to use devise (Note)
Deploy to Ruby on Rails Elastic beanstalk (IAM permission change)
[Rails] How to use flash messages
[rails] How to display db information
How to "hollow" View on Android
[Rails] How to write in Japanese
[Rails] How to prevent screen transition
[Java] How to update Java on Windows
How to install ImageMagick on Windows 10