无法在coffee和rails上使用jquery.validation制定规则

时间:2014-08-27 10:32:48

标签: jquery ruby-on-rails ruby coffeescript jquery-validate

你好漂亮的人!

我很抱歉,如果这个问题对你来说很容易,但我现在已经停留了半天,并且无法真正看出我做错了什么。表格如下:

= simple_form_for(@user, :url => registration_path(resource_name), authenticity_token: true) do |f|
  = hidden_field_tag :accTok, @accTok
  .field
    = f.input :channel_name, :autofocus => true
  .field
    = f.input :name
  .field
    = f.input :email
  .field
    = f.input :password
  .field
    = f.input :password_confirmation
  .clearfix
    = button_to t('registration.register'), 'submit', class: "blue medium right"

正如你所看到的,我正在使用simple_form并设计,如果重要的话。

我一直在寻找表单客户端验证方法,我决定使用'jquery-validation-rails'gem。安装完成后,我只需添加

  $(document).ready ->
    $("#new_user").validate()

到我的js.coffee文件和voila - 验证工作。但它只使用我在模型中设置的验证,而不是我后来制定的任何规则:

  $(document).ready ->
    $("#new_user").validate
      rules:
        user_name:
          required: true
          minlength: 3
          maxlength: 40
      messages:
        user_name:
          required: "message1"
          minlength: "message2"
          maxlength: "whyarentyouworking"

Id是正确的,jquery在点击提交按钮阻止请求时弹出错误气泡。你可能已经发现我对coffeescript不熟悉,但我想开始学习语法。任何想法这个代码有什么问题?对于我可以尝试的任何提示,我将非常感激。

解决方案:正如 Arun P Johny 写下来的那样,我不得不在规则和消息中使用name而不是id。再次感谢你!

0 个答案:

没有答案