# Two root URLs

**URL:** <https://www.ruby-forum.com/t/two-root-urls/154374>\
**Category:** Rails\
**Created:** [November 28, 2008, 12:45pm UTC](https://www.ruby-forum.com/t/two-root-urls/154374 "2008-11-28T12:45:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ratcheer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/aca169/32.png) [@ratcheer](https://www.ruby-forum.com/u/ratcheer)\
**Post date:** [November 28, 2008, 12:45pm UTC](https://www.ruby-forum.com/t/two-root-urls/154374/1 "2008-11-28T12:45:22Z")

</div>

Is it possible to have to have the root url configured so that the user  
is directed to a different page depending if they are logged in or not  
whilst also keeping the url the same?

E.g  
If the user is logged in, I want [www.example.com](http://www.example.com) to show the Users#show  
action  
If the user is not logged in, I want [www.example.com](http://www.example.com) to the the  
Sessions#new action

At the moment I have the root url configured to be the Users#show action  
and I have a ‘login\_required’ before filter on the Users#show action  
which redirects the user to Sessions#new if the user is not logged in.  
This works well but the URL is changed when the user is directed and so  
all nonlogged in users see [www.example.com/login](http://www.example.com/login) as the url when they  
come to the site. I want everybody to see [www.example.com](http://www.example.com) regardless of  
whether they are logged in or not.

routes.rb  
map.root :controller =\> ‘users’, :action =\> ‘show’  
map.login ‘/login’, :controller =\> ‘sessions’, :action =\> ‘new’

Thanks

---

<div class="post-metadata">

**Author:** ![ratcheer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/aca169/32.png) [@ratcheer](https://www.ruby-forum.com/u/ratcheer)\
**Post date:** [November 28, 2008, 1:48pm UTC](https://www.ruby-forum.com/t/two-root-urls/154374/2 "2008-11-28T13:48:28Z")

</div>

Hi Tim,  
Your question was interesting.

> Is it possible to have to have the root url configured so that the user  
> is directed to a different page depending if they are logged in or not  
> whilst also keeping the url the same?
> 
> E.g  
> If the user is logged in, I wantwww.example.comto show the Users#show  
> action  
> If the user is not logged in, I wantwww.example.comto the the  
> Sessions#new action

No, its not possible as far I know.

> map.login ‘/login’, :controller =\> ‘sessions’, :action =\> ‘new’
> 
> ## Thanks
> 
> Posted viahttp://www.ruby-forum.com/.

All files under config directory could not access any methods or  
variables like ‘session’. And it is must to have a different urls for  
different actions rendered in the browser.

Still you want to do that?

Use same controller and same action but render a partial based on your  
stuff needed.

Thanks,  
Sadeesh Kumar Viswanathan.

---

<div class="post-metadata">

**Author:** ![ratcheer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/aca169/32.png) [@ratcheer](https://www.ruby-forum.com/u/ratcheer)\
**Post date:** [November 28, 2008, 7:06pm UTC](https://www.ruby-forum.com/t/two-root-urls/154374/3 "2008-11-28T19:06:54Z")

</div>

Tim,

You might consider a common view with two different partials - one for  
each case.

routes.rb:

map.index ‘/’,  
:controller =\> ‘pages’,  
:action =\> ‘welcome’,  
:id =\> ‘1-welcome-page’

controllers/pages\_controller.rb:

def welcome  
@page = Page.find(params[:id])

```
respond_to do |format|
  format.html # welcome.html.erb
  format.xml { render :xml => @page }
end

```

end

views/pages/welcome.html.erb

**Title:** \<%=h @page.title %\>

\<% if logged\_in? %\>  
\<%= render :partial =\> ‘layouts/welcome\_back’ %\>  
\<% else %\>  
\<%= render :partial =\> ‘layouts/please\_login’ %\>  
\<% end %\>

Rick  
Rick

On Nov 28, 1:45 am, Tim C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
