Need help with some ugly code - is there a better way?

Hi,
In my user_controller.rb, I have the following method, which is supposed
to send the user to their profile, dependng on what “role” they are (the
roles correspond to the other controllers: venue, band, fan):

def login
  if request.post?
    if session[:user_id] = User.authenticate(params[:user][:login], 

params[:user][:password])
flash[:message] = “Login successful”
if session[:user_id].role == ‘Fan’
redirect_to :controller => ‘fan’, :action => ‘profile’, :id
=> session[:user_id].fan.id
else
if session[:user_id].role == ‘Band’
redirect_to :controller => ‘band’, :action => ‘profile’,
:id => session[:user_id].band.id
else
redirect_to :controller => ‘venue’, :action => ‘profile’,
:id => session[:user_id].venue.id
end
end
else
reset_session
flash[:message] = “Incorrect Username and/or password.”
redirect_to :action=>‘login’
end
end
end

That’s pretty ugly! Is there a better way to do this? Is there the
equiv. of a “case” or “switch” statement in RoR?

Do I need to provide more info?
thanks

On Jul 7, 2006, at 4:54 PM, Chad Wells wrote:

[:login],
else
end
end

That’s pretty ugly! Is there a better way to do this? Is there the
equiv. of a “case” or “switch” statement in RoR?

Do I need to provide more info?
thanks

 def login
   if request.post?
     if session[:user_id] = User.authenticate(params[:user]

[:login], params[:user][:password])
flash[:message] = “Login successful”
case session[:user_id].role
when ‘Fan’
redirect_to :controller => ‘fan’, :action =>
‘profile’, :id => session[:user_id].fan.id
when 'Band
redirect_to :controller => ‘band’, :action =>
‘profile’, :id => session[:user_id].band.id
else
redirect_to :controller => ‘venue’, :action =>
‘profile’, :id => session[:user_id].venue.id
end
end
else
reset_session
flash[:message] = “Incorrect Username and/or password.”
redirect_to :action=>‘login’
end
end
end

-Ezra

Chad Wells wrote:

That’s pretty ugly! Is there a better way to do this? Is there the
equiv. of a “case” or “switch” statement in RoR?

Not to be disrespectful, but it’s a rather poor use of this mailing list
to ask about Ruby language features when they are well documented in
print and online. Please take the time to at least look for yourself
before posting to the list. You would have found the case statement
pretty easily.

That said…

user = User.find(session[:user_id])
role = user.role.downcase
redirect_to :controller => role, :action => ‘profile’, :id =>
user.send(role.to_sym).id

Using case statements is too much work when you have messages and
polymorphism to do the job for you. And don’t put the user object in the
session, just put the id and fetch the user object based on that.


Josh S.
http://blog.hasmanythrough.com

Chad Wells wrote:

Hi,
In my user_controller.rb, I have the following method, which is supposed
to send the user to their profile, dependng on what “role” they are (the
roles correspond to the other controllers: venue, band, fan):

def login
  if request.post?
    if session[:user_id] = User.authenticate(params[:user][:login], 

params[:user][:password])
flash[:message] = “Login successful”
if session[:user_id].role == ‘Fan’
redirect_to :controller => ‘fan’, :action => ‘profile’, :id
=> session[:user_id].fan.id
else
if session[:user_id].role == ‘Band’
redirect_to :controller => ‘band’, :action => ‘profile’,
:id => session[:user_id].band.id
else
redirect_to :controller => ‘venue’, :action => ‘profile’,
:id => session[:user_id].venue.id
end
end
else
reset_session
flash[:message] = “Incorrect Username and/or password.”
redirect_to :action=>‘login’
end
end
end

That’s pretty ugly! Is there a better way to do this? Is there the
equiv. of a “case” or “switch” statement in RoR?

Do I need to provide more info?
thanks

I’m new to RoR, but I can give this a try. There is a case statement in
Ruby and someone please correct me if this is incorrect, but you might
try the following:

user_role = session[:user.id].role
case user_role
when ‘fan’
do something
when ‘Band’
do something else
when ‘venue’
do yet something else
else
do something
end

Josh S. wrote:

Not to be disrespectful, but it’s a rather poor use of this mailing list
to ask about Ruby language features when they are well documented in
print and online. Please take the time to at least look for yourself
before posting to the list. You would have found the case statement
pretty easily.

My apologies. Thanks for the tip.