# Getting the default route

**URL:** <https://www.ruby-forum.com/t/getting-the-default-route/90465>\
**Category:** Rails\
**Created:** [March 29, 2007, 12:50am UTC](https://www.ruby-forum.com/t/getting-the-default-route/90465 "2007-03-29T00:50:48Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![leonb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/22d042/32.png) [@leonb](https://www.ruby-forum.com/u/leonb)\
**Post date:** [March 29, 2007, 12:50am UTC](https://www.ruby-forum.com/t/getting-the-default-route/90465/1 "2007-03-29T00:50:48Z")

</div>

Hi,

I’m using this plugin to get the default controller / action:

module ActionController  
module Routing  
def get\_default\_controller

ActionController::Routing::Routes.routes[0].requirements[:controller]  
end  
def get\_default\_action  
ActionController::Routing::Routes.routes[0].requirements[:action]  
end  
end  
end

Is this the best way or is there a more simple / easier way to do  
this?

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![leonb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/22d042/32.png) [@leonb](https://www.ruby-forum.com/u/leonb)\
**Post date:** [March 29, 2007, 1:17am UTC](https://www.ruby-forum.com/t/getting-the-default-route/90465/2 "2007-03-29T01:17:50Z")

</div>

> ```
> def get_default_action
> ActionController::Routing::Routes.routes[0].requirements[:action]
> end
> 
> ```
> 
> end  
> end
> 
> Is this the best way or is there a more simple / easier way to do  
> this?

You could use a named route and then access it with that name…

routes.rb…

map.default\_route ‘’, :controller =\> “home”

views…

link\_to “Home”, default\_route\_url

-philip

---

<div class="post-metadata">

**Author:** ![leonb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/22d042/32.png) [@leonb](https://www.ruby-forum.com/u/leonb)\
**Post date:** [March 29, 2007, 9:30am UTC](https://www.ruby-forum.com/t/getting-the-default-route/90465/3 "2007-03-29T09:30:33Z")

</div>

But this is my default route:  
map.connect ‘’, :controller =\> “dashboard”

Thanks for your reply.

---

<div class="post-metadata">

**Author:** ![leonb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/22d042/32.png) [@leonb](https://www.ruby-forum.com/u/leonb)\
**Post date:** [March 29, 2007, 9:39am UTC](https://www.ruby-forum.com/t/getting-the-default-route/90465/4 "2007-03-29T09:39:34Z")

</div>

Not that your method doesn’t work. But I would like to do it in the  
first way too.

---

<div class="post-metadata">

**Author:** ![leonb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/22d042/32.png) [@leonb](https://www.ruby-forum.com/u/leonb)\
**Post date:** [March 29, 2007, 5:23pm UTC](https://www.ruby-forum.com/t/getting-the-default-route/90465/5 "2007-03-29T17:23:19Z")

</div>

> But this is my default route:  
> map.connect ‘’, :controller =\> “dashboard”

So change it to…

map.default\_route ‘’, :controller =\> “dashboard”

Then in your view do:

link\_to “Dashboard”, default\_route\_url

-philip

---

<div class="post-metadata">

**Author:** ![leonb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/22d042/32.png) [@leonb](https://www.ruby-forum.com/u/leonb)\
**Post date:** [March 29, 2007, 6:54pm UTC](https://www.ruby-forum.com/t/getting-the-default-route/90465/6 "2007-03-29T18:54:39Z")

</div>

Is there any kind of difference between the two routes?

map.connect “/” …  
map.connect “” …

Or does Rails render them both as the root? Anyone? [Hope this isn’t  
threadjacking]

RSL

---

<div class="post-metadata">

**Author:** ![leonb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/22d042/32.png) [@leonb](https://www.ruby-forum.com/u/leonb)\
**Post date:** [March 29, 2007, 11:08pm UTC](https://www.ruby-forum.com/t/getting-the-default-route/90465/7 "2007-03-29T23:08:07Z")

</div>

Perhaps you’ve hinted here at your real problem. Are you thinking  
that you want to do something like add a ‘class’ attribute to the  
link if it’s the current page?

If that’s true, perhaps this helps:

link\_to\_unless\_current(“Dashboard”, :controller =\>  
‘dashboard’, :action =\> ‘index’) do |text|  
link\_to text, {:controller =\> ‘dashboard’, :action =\>  
‘index’}, :class =\> ‘seperate’  
end

If not, perhaps you can explain what you’re TRYING to do rather than  
how to accomplish a tiny task that you think is part of the solution.

-Rob

On Mar 29, 2007, at 4:36 PM, LeonB wrote:

> > map.default\_route ‘’, :controller =\> “dashboard”
> > 
> > > > > Hi,  
> > > > > def get\_default\_action
> > > > 
> > > > routes.rb…
> > > > 
> > > > map.default\_route ‘’, :controller =\> “home”
> > > > 
> > > > views…
> > > > 
> > > > link\_to “Home”, default\_route\_url
> > > > 
> > > > -philip

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![leonb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/22d042/32.png) [@leonb](https://www.ruby-forum.com/u/leonb)\
**Post date:** [March 29, 2007, 10:37pm UTC](https://www.ruby-forum.com/t/getting-the-default-route/90465/8 "2007-03-29T22:37:01Z")

</div>

Thanks Philip. But then how do I get the seperate controller and  
action from that default method? I need it to add a seperate class to  
links with the selected url.
