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.
//= require jquery
//= require rails-ujs
//= require activestorage
//= require_tree .
Change as above. It seems that the order is important.
<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.
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.
https://web-camp.io/magazine/archives/17690 This article was very easy to understand.
Recommended Posts