2

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

user1854802
  • 388
  • 3
  • 14

1 Answers1

4

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" %>
vee
  • 38,255
  • 7
  • 74
  • 78
  • Vinodadhikary, relatively new to Stack.overflow. Given you answered my questions can I help get you points for doing so ? Pierre – user1854802 Aug 12 '13 at 06:33
  • @user1854802, You're welcome. Glad to be of help. Generally when you feel that an answer is useful you upvote that answer and from the list of available answers whichever answer you feel best answers your question you mark it as answer. So yes, you could reward me for this answer! – vee Aug 12 '13 at 06:38
  • Hi Vinodahikary, I accepted the answer. Ia also tried to vote for it but couldn't do so. My understanding is that I only have3 points and it takes 15 points to be ablee to vote up an answer. – user1854802 Aug 12 '13 at 08:13
  • 1
    Hi Vinodahikary, finally have enough points so I can now vote up an answer, which I have done - thanks again - Pierre – user1854802 Nov 06 '13 at 07:02