简体   繁体   English

将 Parsley 与 rails 6 一起使用

[英]Using Parsley with rails 6

I am using parsley for multistep form.我将欧芹用于多步形式。 My rails version is 6.0.3.我的 Rails 版本是 6.0.3。 I have followed below steps to install parsley.我已按照以下步骤安装欧芹。

yarn add parsleyjs

my yarn.lock file:我的 yarn.lock 文件:

parsleyjs@^2.9.2:
  version "2.9.2"
  resolved "https://registry.yarnpkg.com/parsleyjs/-/parsleyjs-2.9.2.tgz#67c96961d371821f2623965fa2cc81a4522874cb"
  integrity sha512-DKS2XXTjEUZ1BJWUzgXAr+550kFBZrom2WYweubqdV7WzdNC1hjOajZDfeBPoAZMkXumJPlB3v37IKatbiW8zQ==
  dependencies:
    jquery ">=1.8.0"

my package.json file:我的 package.json 文件:

{
  "name": "patient_web",
  "private": true,
  "dependencies": {
    "@fortawesome/fontawesome-free": "^5.13.1",
    "@rails/activestorage": "^6.0.0",
    "@rails/ujs": "^6.0.0",
    "@rails/webpacker": "4.2.2",
    "jquery": "^3.5.1",
    "parsleyjs": "^2.9.2",
    "turbolinks": "^5.2.0"
  },
  "version": "0.1.0",
  "devDependencies": {
    "webpack-dev-server": "^3.11.0"
  }
}

here is my webpack/environment.js file: const { environment } = require('@rails/webpacker')这是我的 webpack/environment.js 文件: const { environment } = require('@rails/webpacker')

const webpack = require('webpack')常量 webpack = 要求('webpack')

environment.plugins.prepend('Provide',
  new webpack.ProvidePlugin({
    $: 'jquery/src/jquery',
    jQuery: 'jquery/src/jquery',
    Parsley: 'parsleyjs/src/parsley'
  })
)
module.exports = environment

Here is i have also require in app/javascript/packs/application.js:这是我在 app/javascript/packs/application.js 中还需要的:

require("@rails/ujs").start()
require("turbolinks").start()
require("@rails/activestorage").start()
require("scss/application.scss")
require("jquery")
require("parsleyjs")

I am getting below error on checking form validation by:我在通过以下方式检查表单验证时遇到以下错误:

$(form).parsley().validate()

error:错误:

Uncaught TypeError: $(...).parsley is not a function
    at HTMLButtonElement.<anonymous> (sign_up:75)
    at HTMLButtonElement.dispatch (event.js:328)
    at HTMLButtonElement.elemData.handle (event.js:148)

It would be great if anyone could help me.如果有人可以帮助我,那就太好了。

This could be maybe deprecation, the gem hasnt been updated since 2018. And rails 6 came out 2019 so compatibility could be the problem.这可能是弃用,gem 自 2018 年以来没有更新。rails 6 于 2019 年推出,因此兼容性可能是问题所在。

or you can try creating a folder inside the javascript folder and inside create two files, one where you import the scss and the other where you import the js and then import those files into the application.js file.或者您可以尝试在 javascript 文件夹中创建一个文件夹,并在其中创建两个文件,一个在其中导入 scss,另一个在其中导入 js,然后将这些文件导入到 application.js 文件中。

if the css doesnt load add this to your application html file如果 css 未加载,请将其添加到您的应用程序 html 文件中

<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= stylesheet_pack_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
    

Yes, both link tag and pack tag.是的,链接标签和包装标签。

This appears to be a breaking change with later versions of jquery (eg 3.5.1).这似乎是 jquery(例如 3.5.1)的更高版本的重大更改。 Check your yarn.lock and ensure all jquery dependencies are forced to 3.4.1 or lower.检查您的 yarn.lock 并确保所有 jquery 依赖项都强制为 3.4.1 或更低版本。 This solved the issue for me.这为我解决了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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