ruby-on-rails client_side_validation simple_form javascript error

杀马特。学长 韩版系。学妹 提交于 2019-12-08 02:43:15

问题


Ruby on Rails application and trying to add client_side_validation working and getting two javascript runtime errors when visiting the screen fields.

The errors;

Uncaught TypeError: Cannot call method 'add' of undefined

Uncaught TypeError: Cannot call method 'remove' of undefined

The Gemfile

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

The 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>
   ...

The View - _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>
    ....

The Model

    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/environments/development added

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

and pre-compiled the code

in config/initializers/simple_form set config.browser_validations = true

in config/initializers/client_side_validations

added

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

The error When visiting the Basins page and selecting a Country from the drop down menu select (f.association :country), when I tab to the next field using web browser Chrome and Developers - Java Script Tools Uncaught TypeError Cannot call method 'removed' of undefined

This occurs in generated code - app/assets/javascripts/rails.validations.js about line 175 in module

    window.ClientSideValidations.enablers

I have tried many things to correct this error including changing the include order for app/assets/javascript, re-running the pre-compile, changing the app view, change config.assets.compile = false to true (set back to false now),

Would greatly appreciate any help

thanks Pierre Le Count


回答1:


You also need to add rails.validations.simple_form after rails.validations. You could either add as follows:

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

Or,

In app/assets/javascripts/application.js:

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

Then change javascript_include_tag to following in app/views/layouts/application.html.erb:

<%= javascript_include_tag "application" %>


来源:https://stackoverflow.com/questions/18180392/ruby-on-rails-client-side-validation-simple-form-javascript-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!