Forum: Ruby on Rails unique login

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
140b42eacbb92368d80e1b01bbca9567?d=identicon&s=25 David (Guest)
on 2006-06-09 10:30
(Received via mailing list)
Hello,

I've created my user model which validates against the database for a
proper user/pass combination.  I've set validation options to ensure
that
both a user and password are entered.

I would like to check via an ajax call whether or not the username is
taken.  I haven't been able to find a tutorial covering this topic.  Do
any of you know of one?

Thanks,
David
9d1f5d2d9de70bd9a934f557dc95a406?d=identicon&s=25 Daniel ----- (liquid)
on 2006-06-09 10:55
(Received via mailing list)
observe_field is your friend here.  Alternatively try a search for "live
search" or something similar.

An example (Untested)

<%= text_field "user", "name" %>
<div id="user_name_result" style="display:none"></div>

<%= observe_field "user_name", :url => { :controller => 'user', :action
=>
'check_user_name' },  :update => 'user_name_result', :on =>  'changed',
:complete => "new Effect.Show( 'user_name_result')"  %>

Additionally you can use the link_to_remote options.  eg :loading,
:complete
etc

Then in your controller you have a method

def check_user_name
  @user = User.find_by_name( params[:user][:name] ) || User.new
  if @user.new_record?
     render :inline => "Div contents to indicate that the name is
available.  This will go in the div" and return
  else
     render :inline => "Div contents to indicate that it's taken"
  end
end
77fec2b94c481ad3d2b2135b1f07ef5f?d=identicon&s=25 Marco Lazzeri (Guest)
on 2006-06-09 10:58
(Received via mailing list)
David <lists@...> writes:
[..]
> I've created my user model which validates against the database for a
> proper user/pass combination.  I've set validation options to ensure that
> both a user and password are entered.
>
> I would like to check via an ajax call whether or not the username is
> taken.  I haven't been able to find a tutorial covering this topic.  Do
> any of you know of one?
[..]

What about something like the following (not tested):

In your login view:

<%= text_field 'user', 'username', :onChange => remote_function(:update
=>
'availability_confirmation', :url => {:controller => 'users', :action =>
'check_for_availability'}, :with => "'username='+this.value") %>

<div id="availability_confirmation"> </div>

And in your controller (ie. 'users_controller.rb'):

  def check_for_availability
    if User.find_by_username(params[:username]).empty?
      render :inline => '<span> Oh yeah! Good choice dude!</span>'
    else
      return :inline => '<span> Please, choose another username </span>'
    end
  end

Cheers,
Marco
This topic is locked and can not be replied to.