# Can't Get a Combined Create Account and Login Working

**URL:** <https://www.ruby-forum.com/t/cant-get-a-combined-create-account-and-login-working/136972>\
**Category:** Rails\
**Created:** [May 7, 2008, 9:04pm UTC](https://www.ruby-forum.com/t/cant-get-a-combined-create-account-and-login-working/136972 "2008-05-07T21:04:32Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![rornoob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7bcc69/32.png) [@rornoob](https://www.ruby-forum.com/u/rornoob)\
**Post date:** [May 7, 2008, 9:04pm UTC](https://www.ruby-forum.com/t/cant-get-a-combined-create-account-and-login-working/136972/1 "2008-05-07T21:04:32Z")

</div>

Hi -

I’m having problems getting a my dual purpose create account and login  
page working.

If I try to login, the index method runs and throws errors based on  
some checks I’m running from my user model. I’m pretty sure that the  
problem is in the model, but I don’t know how to tell rails to  
conditionally validate based on which action I’m calling…

– user Model –

#…  
validates\_presence\_of :username  
validates\_presence\_of :email  
validates\_uniqueness\_of :username  
validates\_uniqueness\_of :email  
attr\_accessor :password\_confirmation  
validates\_confirmation\_of :password  
#…

– UserController –

def index  
@user = User.new(params[:user])  
if request.post? and @user.save  
flash.now[:notice] = “Welcome #{@user.username} - you are all  
set”  
@user = User.new  
end  
end

def login  
session[:user\_id] = nil  
session[:user\_username] = nil  
if request.post?  
user = User.authenticate(params[:username], params[:password])  
if user  
session[:user\_id] = user.id  
session[:user\_username] = user.username  
flash[:notice] = “Welcome back #{user.username}!”  
redirect\_to(:action =\> “index”)  
else  
flash.now[:notice] = “Invalid user/password combination”  
end  
end  
end

– index.html.erb –

 \<%= error\_messages\_for 'user' %\> create account \<% form\_for :user do |form| %\> 

username: \<%= form.text\_field :username, :size =\> 40 %\>

e-mail: \<%= form.text\_field :email, :size =\> 40 %\>

password: \<%= form.password\_field :password, :size =\> 40 %\>

confirm: \<%= form.password\_field :password\_confirmation, :size =\> 40 %\>

```
  <%= submit_tag "create account", :controller => 'user', :action

```

=\> ‘index’ %\>  
\<% end %\>

 sign in \<% form\_tag do %\> 

username: \<%= text\_field\_tag :username, params[:user\_name] %\>

password: \<%= password\_field\_tag :password, params[:password] %\>

\<%= submit\_tag "sign in", :controller =\> 'user', :action =\> 'login' %\>

 \<% end %\> 

Thanks in advance!
