[Rails] How to load JavaScript in a specific view

Development environment

・ Ruby: 2.5.7 Rails: 5.2.4 ・ Vagrant: 2.2.7 -VirtualBox: 6.1 ・ OS: macOS Catalina

Premise

The following has been implemented.

Slim introduction

Implementation

1. Edit ʻapplication.js`

Delete =. By doing this, the javascript file in ʻassets / javascripts` will not be read automatically.

application.js


//Change before
//= require_tree

//Change before
// require_tree

2. Edit ʻenvironments`

If you want to reflect it in the development environment, development.rb, Edit production.rb if you want to reflect it in the production environment.

development.rb


Rails.application.configure do
  #Set the javascript file you want to read individually
  config.assets.precompile += ['gmap.js']
  #Note that coffeescript will give an error if you add an extension!
  config.assets.precompile += ['jquery.jpostal']
end

3. Edit the view

slim:~.html.slim


/Set the javascript file you want to load on this page
= javascript_include_tag 'gmap.js'

Recommended Posts

[Rails] How to load JavaScript in a specific view
Apply CSS to a specific View in Ruby on Rails
How to insert a video in Rails
How to implement a like feature in Rails
How to make a follow function in Rails
[Rails] How to load JavaScript in a specific view
Apply CSS to a specific View in Ruby on Rails
How to implement a like feature in Ajax in Rails
How to make JavaScript work on a specific page
[How to insert a video in haml with Rails]
How to write a date comparison search in Rails
[Rails 6] How to set a background image in Rails [CSS]
[Rails] How to display an image in the view
How to introduce jQuery in Rails 6
How to install Swiper in Rails
How to display a graph in Ruby on Rails (LazyHighChart)
[Rails] How to display information stored in the database in view
How to deal with errors in Rails s could not find a JavaScript runtime.
How to change app name in rails
How to use custom helpers in rails
How to use MySQL in Rails tutorial
Steps to set a favicon in Rails
[rails] How to configure routing in resources
How to rename a model with foreign key constraints in Rails
[rails] How to create a partial template
How to implement ranking functionality in Rails
How to publish a library in jCenter
How to use credentials.yml.enc introduced in Rails 5.2
How to write the view when Vue is introduced in Rails?
How to create a query using variables in GraphQL [Using Ruby on Rails]
[Rails] How to log in with a name by adding a devise name column
How to update user edits in Rails Devise without entering a password
Rails: How to write a rake task nicely
Convert to a tag to URL string in Rails
[Rails] How to write when making a subquery
How to display a web page in Java
[Rails] How to use select boxes in Ransack
How to translate Rails into Japanese in general
How to prevent direct URL typing in Rails
How to add a classpath in Spring Boot
How to create a theme in Liferay 7 / DXP
How to separate .scss by controller in Rails
[Rails] How to create a Twitter share button
How to use JQuery in js.erb of Rails6
[Rails] How to use PostgreSQL in Vagrant environment
How to check Rails commands in the terminal
How to automatically generate a constructor in Eclipse
How to write Rails
[Rails / JavaScript / Ajax] I tried to create a like function in two ways.
[Rails] How to get rid of flash messages in a certain amount of time
How to store data simultaneously in a model associated with a nested form (Rails 6.0.0)
How to make a unique combination of data in the rails intermediate table
How to embed JavaScript variables in HTML with Thymeleaf
How to create a Java environment in just 3 seconds
How to set the display time to Japan time in Rails
How to implement guest login in 5 minutes in rails portfolio
[Rails] How to create a signed URL for CloudFront
[Ruby on Rails] How to write enum in Japanese
How to delete a new_record object built with Rails
How to create a Spring Boot project in IntelliJ
I want to use a little icon in Rails
How to manually generate a JWT with Rails Knock