Can't Get a Combined Create Account and Login Working

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!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs