我想学习在轨道上的红宝石,我正在开展一些项目。当我将鼠标悬停在照片上时,我想查看一个解释。我知道,如何使用jquery悬停,但只有rails 5.2 version但我却拥有rails 6.0.2.2 version。现在,如何为rails 6.0.2.2 version使用hover。您可以在屏幕截图上看到错误消息和我的照片。最后感谢您的帮助
Error ScreenShot = [[1]:https://i.stack.imgur.com/CjTUF.png]
jquery悬停的相关代码行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 %>
我在Gemfile中有这样的jquery。Gemfile
gem 'jquery-rails', '~> 4.3', '>=4.3.1'
我找到了解决方案。 Rails 6有一些新的创新。
在我们的Rails应用程序中,运行以下命令以添加jQuery。
$ yarn add jquery
它将把jQuery的依赖关系保存到我们的应用程序中。
现在要确认是否安装了jquery,请检查以下文件
package.json => "jquery": "^3.3.1",
yarn.lock => jquery@^3.3.1:
在environment.js中添加以下代码
const webpack = require('webpack')
environment.plugins.prepend('Provide',
new webpack.ProvidePlugin({
$: 'jquery/src/jquery',
jQuery: 'jquery/src/jquery'
})
)
现在,我们的文件看起来像,
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
需要在application.js文件中使用jquery。它看起来像,
require("@rails/ujs").start()
require("turbolinks").start()
require("@rails/activestorage").start()
require("channels")
require("jquery")
就这些。您可以在项目中使用jquery