# Routes & methods in controllers

**URL:** <https://www.ruby-forum.com/t/routes-methods-in-controllers/95709>\
**Category:** Rails\
**Created:** [June 10, 2006, 1:56am UTC](https://www.ruby-forum.com/t/routes-methods-in-controllers/95709 "2006-06-10T01:56:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![petegajria](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ecae2f/32.png) [@petegajria](https://www.ruby-forum.com/u/petegajria)\
**Post date:** [June 10, 2006, 1:56am UTC](https://www.ruby-forum.com/t/routes-methods-in-controllers/95709/1 "2006-06-10T01:56:59Z")

</div>

still a newbie in rails so bear with me on this one

new test app… rails test  
generate controller main

i add a layout view for main/index & its working ok  
i set the routes as follows  
map.connect ‘’, :controller =\> “Main”

the above is the only route thats active

in the MainController i add in a new method “about”  
with a simple render\_text “hello” in it

refresh the page & i get this error  
No url can be generated for the hash {:action=\>“about”,  
:controller=\>“Main”}

I thought that since the default route is now going to main, shouldnt  
[http://0.0.0.0:3000/about](http://0.0.0.0:3000/about) now show me the about page ?

or is there something else i should be doing in my routes ?

thanks,  
pete

---

<div class="post-metadata">

**Author:** ![petegajria](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ecae2f/32.png) [@petegajria](https://www.ruby-forum.com/u/petegajria)\
**Post date:** [June 10, 2006, 2:12am UTC](https://www.ruby-forum.com/t/routes-methods-in-controllers/95709/2 "2006-06-10T02:12:08Z")

</div>

I believe you want something like this for your routes:

map.connect ‘:action’, :controller =\> ‘Main’

This way when it matches /about with this rule, it will set the symbol  
:action to ‘about’. That’s why the default:

map.connect ‘:controller/:action/:id’

works without explicitly setting :controller, :action, or :id to  
anything.

## HTH, Ryan

View this message in context:  
[http://www.nabble.com/routes---methods-in-controllers-t1764675.html#a4803395](http://www.nabble.com/routes---methods-in-controllers-t1764675.html#a4803395)  
Sent from the RubyOnRails Users forum at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![petegajria](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ecae2f/32.png) [@petegajria](https://www.ruby-forum.com/u/petegajria)\
**Post date:** [June 10, 2006, 4:31pm UTC](https://www.ruby-forum.com/t/routes-methods-in-controllers/95709/3 "2006-06-10T16:31:59Z")

</div>

Hi Ryan,  
Thanks for the quick reply.  
That did work, but if i add in any other controllers .eg foo  
then any action associated with foo will not show up  
So does this mean i have to add in every controller that I create into  
routes  
or is there one generic way.

Pete

Wuher wrote:

> I believe you want something like this for your routes:
> 
> map.connect ‘:action’, :controller =\> ‘Main’
> 
> This way when it matches /about with this rule, it will set the symbol  
> :action to ‘about’. That’s why the default:
> 
> map.connect ‘:controller/:action/:id’
> 
> works without explicitly setting :controller, :action, or :id to  
> anything.
> 
> ## HTH, Ryan
> 
> View this message in context:  
> [http://www.nabble.com/routes---methods-in-controllers-t1764675.html#a4803395](http://www.nabble.com/routes---methods-in-controllers-t1764675.html#a4803395)  
> Sent from the RubyOnRails Users forum at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![petegajria](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ecae2f/32.png) [@petegajria](https://www.ruby-forum.com/u/petegajria)\
**Post date:** [June 10, 2006, 7:08pm UTC](https://www.ruby-forum.com/t/routes-methods-in-controllers/95709/4 "2006-06-10T19:08:32Z")

</div>

Hey Pete,  
Again, I’m not an expert, but this is the impression I’m under:

Your routes file is simply a list of rules that Rails tries to match  
with  
the request url. It starts on the top and stops at the first rule it  
matches. So you want to have your most general rule at the bottom.  
Gernerally, the rule:

‘:controller/:action/:id’ is good to have at the bottom, catching pretty  
much all the other controllers that you haven’t explicitly defined. So  
in  
that case, if you define the controller foo and go to /foo, it will  
catch  
it, setting :controller =\> ‘foo’, :action =\> ‘index’ and :id =\> nil.  
Or,  
/foo/bar would map :controler =\> ‘foo’, :action =\> ‘bar’, and :id =\>  
nil.

I found the wiki page on routes helpful:  
[http://wiki.rubyonrails.com/rails/pages/Routes](http://wiki.rubyonrails.com/rails/pages/Routes)  
Also (found on that page) I like this fairly short guide:  
[http://manuals.rubyonrails.com/read/book/9](http://manuals.rubyonrails.com/read/book/9)

## HTH, Ryan

View this message in context:  
[http://www.nabble.com/routes---methods-in-controllers-t1764675.html#a4809479](http://www.nabble.com/routes---methods-in-controllers-t1764675.html#a4809479)  
Sent from the RubyOnRails Users forum at [Nabble.com](http://Nabble.com).
