简体   繁体   中英

How to use jQuery hover() Method for Ruby on Rails 6.0.2.2 version

I would like to learn ruby on rails and ı'm traying some project. I want to see an explanation when I hover over the photo with the mouse. I know, how can ı use jquery hover but only rails 5.2 version but ı have rails 6.0.2.2 version .Now how can ı use hover for rails 6.0.2.2 version .You can see error message and my photo on the screenshot. And finally thank you for your help

Error ScreenShot = [[1]: https://i.stack.imgur.com/CjTUF.png]

related code line for jquery Hover Shot.js

shotHover() {
                $('.shot').hover(function() {
                    $(this).children('.shot-data').toggleClass('visible');
                });
            }

Shots.shotHover();

application.js

// This file is automatically compiled by Webpack, along with any other files
// present in this directory. You're encouraged to place your actual application logic in
// a relevant structure within app/javascript and only use these pack files to reference
// that code so it'll be compiled.

require("@rails/ujs").start()
require("turbolinks").start()
require("@rails/activestorage").start()
require("channels")
require("shots")



// Uncomment to copy all static images under ../images to the output folder and reference
// them with the image_pack_tag helper in views (e.g <%= image_pack_tag 'rails.png' %>)
// or the `imagePath` JavaScript helper below.
//
// const images = require.context('../images', true)
// const imagePath = (name) => images(name, true)

index.html.erb

<%= link_to shot, class: "shot" do %>
          <%= image_tag(shot.user_shot_url) %>
          <div class="shot-data">
            <h3 class="shot-title"><%= shot.title %></h3>
            <div class="shot-description"><%= truncate(shot.description, length: 60) %></div>
            <div class="shot-time">
              <%= time_ago_in_words(shot.created_at) %>
            </div>
          </div>
        <% end %>

I have jquery in Gemfile like this. Gemfile

gem 'jquery-rails', '~> 4.3', '>=4.3.1'

I found solution. Rails 6 have some new innovation.

In our Rails application run below command to add jQuery.

$ yarn add jquery

It will save the dependency of jquery to our application.

Now to verify jquery is installed or not, check below files

package.json => "jquery": "^3.3.1",
yarn.lock => jquery@^3.3.1:

Add below code in environment.js

const webpack = require('webpack')
environment.plugins.prepend('Provide',
  new webpack.ProvidePlugin({
    $: 'jquery/src/jquery',
    jQuery: 'jquery/src/jquery'
  })
)

Now, our file looks like,

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

Require jquery in application.js file. it's looks like,

require("@rails/ujs").start()
require("turbolinks").start()
require("@rails/activestorage").start()
require("channels")
require("jquery")

That's all.You can use jquery on your project

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM