# Unique login

**URL:** <https://www.ruby-forum.com/t/unique-login/95689>\
**Category:** Rails\
**Created:** [June 9, 2006, 10:30am UTC](https://www.ruby-forum.com/t/unique-login/95689 "2006-06-09T10:30:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [June 9, 2006, 10:30am UTC](https://www.ruby-forum.com/t/unique-login/95689/1 "2006-06-09T10:30:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [June 9, 2006, 10:55am UTC](https://www.ruby-forum.com/t/unique-login/95689/2 "2006-06-09T10:55:15Z")

</div>

observe\_field is your friend here. Alternatively try a search for “live  
search” or something similar.

An example (Untested)

\<%= text\_field “user”, “name” %\>

\<%= 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

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [June 9, 2006, 10:58am UTC](https://www.ruby-forum.com/t/unique-login/95689/3 "2006-06-09T10:58:26Z")

</div>

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”) %\>

 

And in your controller (ie. ‘users\_controller.rb’):

def check\_for\_availability  
if User.find\_by\_username(params[:username]).empty?  
render :inline =\> ’ Oh yeah! Good choice dude!’  
else  
return :inline =\> ’ Please, choose another username ’  
end  
end

Cheers,  
Marco
