Topic: Check user availability with jQuery

After reading this article today, I decided I wanted to do this for jquery and rails.

I started to hit up google, looking to see if anyone has already done it for me. I found someone doing what I wanted with php, so I adapted it for rails.

The original article can be found here.

I figured out the below and made this quick tutorial. I'm sure there are ways to improve upon the code below. Anyone who can, please share.

Using restful_authentication, and haml for forms.

First create the basic user signup form, along with the added msgbox span that the javascript uses to insert html.

[code=views/users/new.html.haml]
- form_for :user, :url => users_path do |f|
  %fieldset
    %ol
      %li
        %label{:for => "login"} Login:
        = f.text_field :login
        %span#msgbox
      %li
        %label{:for => "password"} Password:
        = f.password_field :password
      %li
        %label{:for => "password_confirmation"} Confirm Password:
        = f.password_field :password_confirmation
      %li
        %label{:for => "signup"}
        = submit_tag 'Sign up'
[/code]

Next in the users controller, create a new method to return true or false, based on if the user exists or not.

[code=controllers/users_controller.rb]
protect_from_forgery :except => :checkuser

def checkuser
  @user = User.find_by_login(params[:user_login])
  if @user
    render :text => true, :layout => false
  elsif params[:user_login].blank?
    render :text => true, :layout => false
  else
    render :text => false, :layout => false
  end
end
[/code]

If the user exists return true, if not return false. Other than that the only check needed is if the user entered something. Currently if a user enters a . the code will validate as username available. This really needs to be improved. Maybe someone can provide a way to check this using rails validations? Either way the username will be validated when submitted.

protect_from_forgery is disabled here as it's not needed. Username availability is the only thing that needs to be checked, no data is submitted to the database.

[code=public/javascripts/application.js]
$(document).ready(function() {
  $("#user_login").blur(function() {
    //remove all the class add the messagebox classes and start fading
    $("#msgbox").removeClass().addClass('messagebox').text('Checking...').fadeIn("slow");
    //check the username exists or not from ajax
    $.post("/checkuser",{ user_login:$(this).val() } ,function(data) {
      if(data=='true') { //if username not avaiable
        $("#msgbox").fadeTo(200,0.1,function() { //start fading the messagebox
          //add message and change the class of the box and start fading
          $(this).html('This User name Already exists').addClass('messageboxerror').fadeTo(900,1);
        });
        } else {
          $("#msgbox").fadeTo(200,0.1,function() {  //start fading the messagebox
          //add message and change the class of the box and start fading
          $(this).html('Username available to register').addClass('messageboxok').fadeTo(900,1);
        });
      }
    });
  });
});
[/code]

Now check the response from the users controller and display the message. Read the comments.

Finally a route is added to handle the above.

[code=config/routes.rb]
map.checkuser '/checkuser', :controller => 'users', :action => 'checkuser', :method => :get
[/code]

Re: Check user availability with jQuery

very, very interesting.

i will be using/needing this in the near future. thanks for sharing greg.

Re: Check user availability with jQuery

If you feel like protecting from forgery.

http://thenexttrain.co.za/2008/10/ruby- … t-forgery/