简体   繁体   English

jQuery验证+ Rails没有触发

[英]jQuery validation + rails isn't firing

I'm using 我正在使用

Rails: 5.1.4 滑轨:5.1.4

Ruby: 2.4.1 红宝石:2.4.1

I'm currently trying to get jQuery validation working with my rails app but I think the reason why it's not working is that when I inspect my code I'm getting an Uncaught TypeError: Cannot read property 'addMethod' of undefined error. 我当前正在尝试使jQuery验证与我的rails应用程序一起工作,但是我认为它不起作用的原因是,当我检查代码时遇到了Uncaught TypeError: Cannot read property 'addMethod' of undefined错误的Uncaught TypeError: Cannot read property 'addMethod' of undefined

Now I have these gems installed: 现在,我安装了以下宝石:

gem 'jquery-validation-rails' gem'jquery-validation-rails'

gem 'jquery-rails' 宝石'jquery-rails'

Here is my application.js file: 这是我的application.js文件:

//= require jquery
//= require jquery_ujs
//= require jquery.validate.additional-methods
//= require jquery.validate
//= require popper
//= require bootstrap-sprockets
//= require trix
//= require rails-ujs
//= require local-time
//= require turbolinks
//= require_tree .

Here is the validation jQuery I have so far: 这是到目前为止的验证jQuery:

<script type="text/javascript">
 $(document).ready(function () {
 $("new-listing-form").validate(
 {
   rules: {
     listing_title: {
       required: true
     }
   }
 }
 );
 });
 </script>

I'm trying to test the validation on this form field: 我正在尝试在此表单字段上测试验证:

<%= form.text_field :title, id: :listing_title, class: 'form-
 control', placeholder: 'What is the job title?' %>

What do you think is the matter with all of this? 您如何看待所有这一切?

Thank you ahead of time! 提前谢谢你!

1 - If you are using turbolinks then must use 1-如果使用的是Turbolink,则必须使用

$(document).on("turbolinks:load", function(){
  //the code
});

see turbolinks events https://github.com/turbolinks/turbolinks 看到turbolinks事件https://github.com/turbolinks/turbolinks

2- Like "new-listing-form" is not a tag 2-像“新列表形式”不是标签

 $("new-listing-form")

never will find nothing 永远不会找到任何东西

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

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