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 --
<div class=""> <%= error_messages_for 'user' %> <fieldset> <legend>create account</legend> <% form_for :user do |form| %> <p> <label for="user_name">username:</label> <%= form.text_field :username, :size => 40 %> </p> <p> <label for="user_email">e-mail:</label> <%= form.text_field :email, :size => 40 %> </p> <p> <label for="user_password">password:</label> <%= form.password_field :password, :size => 40 %> </p> <p> <label for="user_password_confirmation">confirm:</label> <%= form.password_field :password_confirmation, :size => 40 %> </p>
<%= submit_tag "create account", :controller => 'user', :action => 'index' %> <% end %> </fieldset> </div>
<div class=""> <fieldset> <legend>sign in</legend> <% form_tag do %> <p> <label for="user_name">username:</label> <%= text_field_tag :username, params[:user_name] %> </p> <p> <label for="password">password:</label> <%= password_field_tag :password, params[:password] %> </p> <p> <%= submit_tag "sign in", :controller => 'user', :action => 'login' %> </p> <% end %> </fieldset> </div>
Thanks in advance!