Chad Wells
2006-Jul-07 23:55 UTC
[Rails] 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 -- Posted via http://www.ruby-forum.com/.
Ezra Zygmuntowicz
2006-Jul-08 00:11 UTC
[Rails] need help with some ugly code - is there a better way?
On Jul 7, 2006, at 4:54 PM, 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? > thanksdef 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
Josh Susser
2006-Jul-08 00:20 UTC
[Rails] Re: need help with some ugly code - is there a better way?
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 Susser http://blog.hasmanythrough.com -- Posted via http://www.ruby-forum.com/.
Scott Helfrich
2006-Jul-08 00:20 UTC
[Rails] Re: need help with some ugly code - is there a better way?
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? > thanksI''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 -- Posted via http://www.ruby-forum.com/.
Chad Wells
2006-Jul-08 00:26 UTC
[Rails] Re: need help with some ugly code - is there a better way?
Josh Susser 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. -- Posted via http://www.ruby-forum.com/.