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

**URL:** <https://www.ruby-forum.com/t/need-help-with-some-ugly-code-is-there-a-better-way/97574>\
**Category:** Rails\
**Created:** [July 8, 2006, 1:54am UTC](https://www.ruby-forum.com/t/need-help-with-some-ugly-code-is-there-a-better-way/97574 "2006-07-08T01:54:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![superwick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/46a35a/32.png) [@superwick](https://www.ruby-forum.com/u/superwick)\
**Post date:** [July 8, 2006, 1:54am UTC](https://www.ruby-forum.com/t/need-help-with-some-ugly-code-is-there-a-better-way/97574/1 "2006-07-08T01:54:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![superwick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/46a35a/32.png) [@superwick](https://www.ruby-forum.com/u/superwick)\
**Post date:** [July 8, 2006, 2:13am UTC](https://www.ruby-forum.com/t/need-help-with-some-ugly-code-is-there-a-better-way/97574/2 "2006-07-08T02:13:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![superwick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/46a35a/32.png) [@superwick](https://www.ruby-forum.com/u/superwick)\
**Post date:** [July 8, 2006, 2:19am UTC](https://www.ruby-forum.com/t/need-help-with-some-ugly-code-is-there-a-better-way/97574/3 "2006-07-08T02:19:56Z")

</div>

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](http://blog.hasmanythrough.com)

---

<div class="post-metadata">

**Author:** ![superwick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/46a35a/32.png) [@superwick](https://www.ruby-forum.com/u/superwick)\
**Post date:** [July 8, 2006, 2:20am UTC](https://www.ruby-forum.com/t/need-help-with-some-ugly-code-is-there-a-better-way/97574/4 "2006-07-08T02:20:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![superwick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/46a35a/32.png) [@superwick](https://www.ruby-forum.com/u/superwick)\
**Post date:** [July 8, 2006, 2:26am UTC](https://www.ruby-forum.com/t/need-help-with-some-ugly-code-is-there-a-better-way/97574/5 "2006-07-08T02:26:10Z")

</div>

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.
