# Rails ROUTING map.root

**URL:** <https://www.ruby-forum.com/t/rails-routing-map-root/187919>\
**Category:** Rails\
**Created:** [May 2, 2010, 4:40am UTC](https://www.ruby-forum.com/t/rails-routing-map-root/187919 "2010-05-02T04:40:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidz101](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f0a364/32.png) [@davidz101](https://www.ruby-forum.com/u/davidz101)\
**Post date:** [May 2, 2010, 4:40am UTC](https://www.ruby-forum.com/t/rails-routing-map-root/187919/1 "2010-05-02T04:40:07Z")

</div>

Is it possible to put an if/else statemnt, and change the map.root  
under each condition? for ex–

if current\_user  
map.root :controller =\> ‘current\_user’

else

map.root :controller =\> ‘home’

end

cuase apparenlty that doesn’t work, so is there an alternative way?  
basically if the user is logged in, i dont want the homepage to be the  
homepage anymore, but instead the user page.

NOTE:::: i can’t do a redirect\_to in my home controller, because i  
have multiple cases (current\_user, current\_admin, etc) and i can’t  
have multiple redirects in an action.

so is there a way to change the homepage according to who’s logged in?  
Thanks

---

<div class="post-metadata">

**Author:** ![davidz101](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f0a364/32.png) [@davidz101](https://www.ruby-forum.com/u/davidz101)\
**Post date:** [May 3, 2010, 12:34am UTC](https://www.ruby-forum.com/t/rails-routing-map-root/187919/2 "2010-05-03T00:34:01Z")

</div>

Hi David,

Your conditional routing won’t work because routes.rb is evaluated on  
startup not per request.

I don’t understand the rest of the question. You can’t redirect  
multiple times per action but you can have multiple possible redirects  
in an action as long as only one is executed - does that help?

/Ritchie

---

<div class="post-metadata">

**Author:** ![davidz101](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f0a364/32.png) [@davidz101](https://www.ruby-forum.com/u/davidz101)\
**Post date:** [May 3, 2010, 1:09pm UTC](https://www.ruby-forum.com/t/rails-routing-map-root/187919/3 "2010-05-03T13:09:12Z")

</div>

Hi,  
During processing of request first it executes routes. so, it’s not  
possible to add above type of conditional statement (authentication).  
For conditional routing refer -  
[http://www.buildingwebapps.com/articles/7082-enhancing-conditional-routing-in-rails](http://www.buildingwebapps.com/articles/7082-enhancing-conditional-routing-in-rails)

---

<div class="post-metadata">

**Author:** ![davidz101](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f0a364/32.png) [@davidz101](https://www.ruby-forum.com/u/davidz101)\
**Post date:** [May 5, 2010, 2:09am UTC](https://www.ruby-forum.com/t/rails-routing-map-root/187919/4 "2010-05-05T02:09:43Z")

</div>

Ritchie,

you wrote “You can’t redirect  
multiple times per action but you can have multiple possible redirects  
in an action as long as only one is executed”

so how do i do that? An if/else statment doesn’t work in this case, is  
there another way?

---

<div class="post-metadata">

**Author:** ![davidz101](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f0a364/32.png) [@davidz101](https://www.ruby-forum.com/u/davidz101)\
**Post date:** [May 6, 2010, 1:07am UTC](https://www.ruby-forum.com/t/rails-routing-map-root/187919/5 "2010-05-06T01:07:10Z")

</div>

right, i understand…thats what i did

heres my code-

if current\_user

redirect\_to current\_user\_path

elseif current\_admin

redirect\_to current\_admin\_path

elseif current\_secretary  
redirect\_to current\_secretary\_path

else  
@greeting = “Hello Guest”  
end

but its saying that i have multiple rediects per action, and i do, but  
apparently rails thinks all of them will get executed? thats my  
error :-\

any suggestions? thanks

---

<div class="post-metadata">

**Author:** ![davidz101](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f0a364/32.png) [@davidz101](https://www.ruby-forum.com/u/davidz101)\
**Post date:** [May 5, 2010, 10:04am UTC](https://www.ruby-forum.com/t/rails-routing-map-root/187919/6 "2010-05-05T10:04:57Z")

</div>

On May 5, 1:08 am, David Z. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Ritchie,
> 
> you wrote “You can’t redirect  
> multiple times per action but you can have multiple possible redirects  
> in an action as long as only one is executed”
> 
> so how do i do that? An if/else statment doesn’t work in this case, is  
> there another way?

Well in a nutshell

if current\_user  
redirect\_to user\_path(current\_user)  
else  
redirect\_to home\_path  
end

(assuming the existance of various named routes)

Show us what you’ve attempted.

Fred
