# Standard route but having errors

**URL:** <https://www.ruby-forum.com/t/standard-route-but-having-errors/124922>\
**Category:** Rails\
**Created:** [January 3, 2008, 8:00pm UTC](https://www.ruby-forum.com/t/standard-route-but-having-errors/124922 "2008-01-03T20:00:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chubbs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/47e85d/32.png) [@Chubbs](https://www.ruby-forum.com/u/Chubbs)\
**Post date:** [January 3, 2008, 8:00pm UTC](https://www.ruby-forum.com/t/standard-route-but-having-errors/124922/1 "2008-01-03T20:00:19Z")

</div>

hey i have routes defined as,

map.question ‘question/:id’, :controller =\> ‘question’, :action =\>  
‘show’

# Install the default route as the lowest priority.

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

now im having errors trying to get any of the actions inside my  
question controller.  
question/new  
question/edit  
etc… all come up with errors trying to send it to the action ‘show’

if i reverse the order:

map.connect ‘:controller/:action/:id.:format’  
map.connect ‘:controller/:action/:id’  
map.question ‘question/:id’, :controller =\> ‘question’, :action =\>  
‘show’

my actions work but then if i use question/2 it says  
Unknown action

No action responded to 2

how do i route my urls properly for this to work?

---

<div class="post-metadata">

**Author:** ![Chubbs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/47e85d/32.png) [@Chubbs](https://www.ruby-forum.com/u/Chubbs)\
**Post date:** [January 3, 2008, 8:10pm UTC](https://www.ruby-forum.com/t/standard-route-but-having-errors/124922/2 "2008-01-03T20:10:17Z")

</div>

2008/1/3, Chubbs [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> hey i have routes defined as,
> 
> map.question ‘question/:id’, :controller =\> ‘question’, :action =\>  
> ‘show’
> 
> # Install the default route as the lowest priority.
> 
> map.connect ‘:controller/:action/:id.:format’  
> map.connect ‘:controller/:action/:id’  
> […]  
> how do i route my urls properly for this to work?

Try :

map.question ‘question/:id’, :controller =\> ‘question’, :action =\>  
‘show’, :id =\> /\d+/  
map.connect ‘:controller/:action/:id.:format’  
map.connect ‘:controller/:action/:id’

```
-- Jean-François.

```

---

<div class="post-metadata">

**Author:** ![Chubbs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/47e85d/32.png) [@Chubbs](https://www.ruby-forum.com/u/Chubbs)\
**Post date:** [January 3, 2008, 8:12pm UTC](https://www.ruby-forum.com/t/standard-route-but-having-errors/124922/3 "2008-01-03T20:12:39Z")

</div>

ok cool that worked,

but why is the :id =\> /\d+/ needed? and what does that do ?  
does it check to see if its a decimal as a parameter or something ?

---

<div class="post-metadata">

**Author:** ![Chubbs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/47e85d/32.png) [@Chubbs](https://www.ruby-forum.com/u/Chubbs)\
**Post date:** [January 3, 2008, 8:26pm UTC](https://www.ruby-forum.com/t/standard-route-but-having-errors/124922/4 "2008-01-03T20:26:46Z")

</div>

2008/1/3, Chubbs [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> ok cool that worked,
> 
> but why is the :id =\> /\d+/ needed? and what does that do ?  
> does it check to see if its a decimal as a parameter or something ?

yes.

when you’ve got this named route :  
map.question ‘question/:id’, :controller =\> ‘question’, :action =\>  
‘show’

with ‘question/new’, Rails will try to match this path with your route,  
and it will be successful with params[:id] = ‘new’.

If you require that the dynamic segment (:id) in the route must be in  
decimal format ( :id =\> /\d+/), then ‘question/new’ will not be  
recognized by the question named route, and Rails will try  
with next route and so on. ‘question/new’ will be later recognized  
by ‘:controller/:action/:id’ route.

Requirements on route can be useful.

– Jean-François.

---

<div class="post-metadata">

**Author:** ![Chubbs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/47e85d/32.png) [@Chubbs](https://www.ruby-forum.com/u/Chubbs)\
**Post date:** [January 3, 2008, 8:31pm UTC](https://www.ruby-forum.com/t/standard-route-but-having-errors/124922/5 "2008-01-03T20:31:33Z")

</div>

mm cos i thought rails would put preference to actions if one exists  
so i thought it would work like this.

if we have ‘question/123’

look for an action by the name of 123 , if not then see what other  
routes we have, in this case we have  
map.question ‘question/:id’, :controller =\> ‘question’, :action =\>  
‘show’

if we have ‘question/new’

we found an action so ignore the other routes

---

<div class="post-metadata">

**Author:** ![Chubbs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/47e85d/32.png) [@Chubbs](https://www.ruby-forum.com/u/Chubbs)\
**Post date:** [January 3, 2008, 8:57pm UTC](https://www.ruby-forum.com/t/standard-route-but-having-errors/124922/6 "2008-01-03T20:57:40Z")

</div>

2008/1/3, Chubbs [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> if we have ‘question/new’
> 
> we found an action so ignore the other routes

Not exactly. The way it works, as far as I know, is that Rails  
tries to make your path recognize from routes, from the top to  
the bottom, and stops when there is a match. If any routes don’t  
recognize your path, there’s an error. If a path can be recognized  
by many routes, the one with the highest priority wins (because  
Rails starts at the top and stop at the first match). So the order  
of your routes is important.

Also :

- in your class controller, it’s possible to use a method\_missing  
mechanism
- it’s also possible that the method doesn’t exist but  
as the template exists, Rails will directly render it (I never use  
this mechanism but it should work)
- and in your routes.rb, you can if you want, suppress the generic  
route :

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

because you find it permissive and you’d rather defining all the  
routes ‘statically’.

– Jean-François.

---

<div class="post-metadata">

**Author:** ![Chubbs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/47e85d/32.png) [@Chubbs](https://www.ruby-forum.com/u/Chubbs)\
**Post date:** [January 3, 2008, 9:02pm UTC](https://www.ruby-forum.com/t/standard-route-but-having-errors/124922/7 "2008-01-03T21:02:34Z")

</div>

ok thanks heaps for your advice.

have good day!
