5

I am using client_side_validations gem to perform a form validation in rails 3.

everything works fine except when the firefox/ie throws javascript alert when unique validation ajax runs and return 404 error message.

The author says a 404 means record not found and therefore unique;

Is there any way we can handle this.

I have opened this in : https://github.com/bcardarella/client_side_validations/issues/297

dbKooper
  • 1,035
  • 10
  • 17

2 Answers2

5

The error you are seeing is because the remote validation cannot find the resource on the server. This is expected, if the resource is not found the resource is unique.

While I think this is the semantically correct status code to return and I do not think the browsers should be showing errors for this I am clearly going to lose. So in the next version of ClientSideValidations I will be changing this behavior to return a status code in the 2xx range. Sorry for the confusion.

bcardarella
  • 4,667
  • 4
  • 29
  • 45
  • any timeframe on this fix? I also see errors in the chrome console. – plainjimbo Apr 23 '12 at 21:37
  • "soonish"? I'm pretty swamped at the moment. I know it's a lame answer, sorry :( – bcardarella Apr 24 '12 at 17:25
  • I have written the code to handle all Ajax exception commonly in my application.js file. But everytime I am getting **Requested page not found. [404]** alert when a unique validation is run. Any solution ? `$.ajaxSetup({ error: function(jqXHR, exception) { if (jqXHR.status === 0) { alert('Not connect.\n Verify Network.'); } else if (jqXHR.status == 404) { alert('Requested page not found. [404]'); } else if (jqXHR.status == 500) { alert('Internal Server Error [500].'); } } });` – Manivannan Jeganathan Jan 31 '13 at 07:30
0

I had the same issue which caused some problems as the application I am working on has global toast messages that come up for errors such as 404 on ajax calls.

That being the case, I made this patch. You can certainly do more. It would be a good idea, for example, to change how the javascript works, but this monkey patch is the smallest change I could make to the code and still get the required functionality. Please note, HTTP 204 is used because the ClientSideValidations gem expects all HTTP 200 (very specifically 200) responses to be validation failed responses and, so, couldn't be reused without also changing the javascript.

app/config/initializers/client_side_validations.rb

ClientSideValidations::Config.disabled_validators = []

# Monkey Patch

# Guranatee uniqueness middleware is fully loaded
::ClientSideValidations::Middleware::Uniqueness

module ClientSideValidations
  module Middleware
    class Uniqueness
      def response
        begin
          if unique?
            self.status = 204 # changed from 404
            self.body   = '' # changed from true
          else
            self.status = 200
            self.body   = 'false'
          end
        rescue NotValidatable
          self.status = 500
          self.body = ''
        end
        super
      end
    end
  end
end
Chad M
  • 943
  • 1
  • 9
  • 22