Question

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

Was it helpful?

Solution

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" %>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top