ruby-on-rails client_side_validation simple_form javascript错误

时间:2013-08-12 05:56:21

标签: ruby-on-rails simple-form client-side-validation

Ruby on Rails应用程序,并尝试在访问屏幕字段时添加client_side_validation并获取两个javascript运行时错误。

错误;

未捕获的TypeError:无法调用未定义的方法'add'

未捕获的TypeError:无法调用未定义的方法'remove'

Gemfile

      source 'https://rubygems.org'
      ........
      gem 'simple_form'
      gem 'client_side_validations'
      gem 'client_side_validations-simple_form'
      .....

application.html.erb

    <!DOCTYPE html>
    <html>
    <head>
    <title><%= content_for?(:title) ? yield(:title) : "Drill Investor" %></title>
    <!--[if lt IE 9]><script src="http://html5shim.googlecode.com/svn/trunk/html5.js"     type="text/javascript"></script><![endif]-->
   <%= stylesheet_link_tag    "application", :media => "all" %>
   <%= javascript_include_tag "application", "rails.validations"%>
   <%= csrf_meta_tags %>
   </head>
   <body>
   ...

视图 - _form.html.erb

    <%= simple_form_for (@basin), :validate => true do |f| %>
      <%= f.error_notification %>
      <div class="form-inputs">
          <%= f.association :country %>
          <%= f.input :name %>
      </div>
    ....

模特

    class Basin < ActiveRecord::Base
      include ActiveModel::ForbiddenAttributesProtection
      belongs_to :country
      has_many :drills
      validates :country_id, :name, presence: true
      validates :name, uniqueness: {scope: [:country_id],
        message: "A country can only have one basin with this name"}
    end

配置/环境/发育 加入

    config.assets.precompile += %w( rails.validations.js )

并预编译代码

在config / initializers / simple_form中设置config.browser_validations = true

在config / initializers / client_side_validations

添加了

    require 'client_side_validations/simple_form' if defined?(::SimpleForm)

错误 访问Basins页面并从下拉菜单中选择Country时选择(f.association:country),当我使用Web浏览器选项卡到下一个字段时Chrome和开发人员 - Java Script Tools 未捕获的TypeError无法调用未定义的方法“已删除”

这发生在生成的代码中 - app / assets / javascripts / rails.validations.js 关于模块中的第175行

    window.ClientSideValidations.enablers

我已经尝试了很多方法来纠正这个错误,包括更改app / assets / javascript的include顺序,重新运行预编译,更改app视图,将config.assets.compile = false更改为true(退回现在为假),

非常感谢任何帮助

感谢 Pierre Le Count

1 个答案:

答案 0 :(得分:4)

您还需要在rails.validations.simple_form之后添加rails.validations。你可以添加如下:

<%= javascript_include_tag "application", "rails.validations", "rails.validations.simple_form" %>

或者,

在app / assets / javascripts / application.js中:

//= require rails.validations
//= require rails.validations.simple_form 

然后在app / views / layouts / application.html.erb中将javascript_include_tag更改为以下内容:

<%= javascript_include_tag "application" %>