0

I have this problem : I'm checking user email in controller and sending json successfull response if it is already taken and add css styling of input, also I need to prevent submit and add some message.

Here is my checkemail action ( used this article - http://paydrotalks.com/posts/45-standard-json-response-for-rails-and-jquery ):

def checkemail
  if !User.where('email = ?', params[:email]).empty?
    format.jsonr do
      render :json => { 
        :status => :ok, 
        :message => "Success!",
      }.to_json
    end
  end
end

and in my routes:

         checkemail GET    /checkemail(.:format)

          match "/checkemail", to: 'edit_user#checkemail'

and my jQuery:

   $('#user_email').focusout(function() {
    $.getJSON('/checkemail', { email: $('#user_email').val() }, function(data) {
        $('#user_email').addClass("error");
      });
   });

my HTML input code:

     <input id="user_email" name="user[email]" size="30" type="email" value="">

from mime_type initializers file:

    Mime::Type.register_alias "application/json", :jsonr, %w( text/x-json )

From Fiddler:

  Request : GET /checkemail?user@mail.com HTTP/2.0

  Response(RAW tab) : 
  Missing template edit_user/checkemail

  JSON tab is empty

QUESTIONS: How to inspect is json request sent ? How can I prevent submission form and add some message ?

MID
  • 1,815
  • 4
  • 29
  • 40

1 Answers1

1

I would do it like this:

$('#element').focusout(function() {
  $.getJSON('PATH_TO_EMAIL_CHECK_ACTION', { email: $('#element').val() }, function(data) {
    // The data should tell you if its unique or not, you do something
    // here depending on response 
  });
});

And in the controller, check if the email is taken. If its not, send back render json with status OK and message success. You can check if the status is success in your callback where the comment is above.

jquery focusout - http://api.jquery.com/focusout/

jquery getJSON - http://api.jquery.com/jQuery.getJSON/

About json responses in controller - http://paydrotalks.com/posts/45-standard-json-response-for-rails-and-jquery

AJcodez
  • 31,780
  • 20
  • 84
  • 118
  • I tried you approach, but failing to implement. Can you look at my code ? – MID Sep 12 '12 at 06:42
  • Please, look here - http://stackoverflow.com/questions/12382680/how-to-send-json-response-from-controller-to-jquery-and-inspect-it – MID Sep 12 '12 at 09:21
  • Hmm. In checkemail try doing a flash message and then refresh the page to see it. To disable/enable submit button - http://stackoverflow.com/questions/1237896/how-to-disable-submit-button-with-jquery – AJcodez Sep 12 '12 at 10:02
  • I'm implemeting it on modal, or you suggest it for testing ? – MID Sep 12 '12 at 10:05
  • Oh also you can log your data in the callback to getJSON. Or just alert it. – AJcodez Sep 12 '12 at 10:05
  • I can alert it using `alert($.getJSON)` or how ? – MID Sep 12 '12 at 10:10
  • `$.getJSON('/checkemail', { email: $('#user_email').val() }, function(data) { alert( data ); });` – AJcodez Sep 12 '12 at 10:31
  • No alert box. It seems to be not executing. – MID Sep 12 '12 at 10:37
  • try just rendering the json no matter what for checkemail and see if it receives something – AJcodez Sep 12 '12 at 10:50
  • When I go to `http://127.0.0.1:3000/checkemail.jsonr` shows me `{"status":"ok","message":"Success!"}` – MID Sep 12 '12 at 10:51