2

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

错误;

未捕获的类型错误:无法调用未定义的方法“添加”

未捕获的类型错误:无法调用未定义的方法“删除”

宝石文件

      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)

错误当访问盆地页面并从下拉菜单中选择一个国家时,选择 (f.association :country),当我使用 Web 浏览器 Chrome 和开发人员选项卡到下一个字段时 - Java Script Tools Uncaught TypeError Cannot call method 'removed'未定义的

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

    window.ClientSideValidations.enablers

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

非常感谢任何帮助

谢谢皮埃尔·勒伯爵

4

1 回答 1

4

您还需要rails.validations.simple_formrails.validations. 您可以添加如下:

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

或者,

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

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

然后javascript_include_tag在 app/views/layouts/application.html.erb 中改为如下:

<%= javascript_include_tag "application" %>
于 2013-08-12T06:03:45.487 回答