# Routing requirements broken in Rails 1.1.4?

**URL:** <https://www.ruby-forum.com/t/routing-requirements-broken-in-rails-1-1-4/98509>\
**Category:** Rails\
**Created:** [July 15, 2006, 7:43am UTC](https://www.ruby-forum.com/t/routing-requirements-broken-in-rails-1-1-4/98509 "2006-07-15T07:43:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [July 15, 2006, 7:43am UTC](https://www.ruby-forum.com/t/routing-requirements-broken-in-rails-1-1-4/98509/1 "2006-07-15T07:43:33Z")

</div>

Hi,

I have the following routes

map.connect ‘:controller/:id/:action’, :id=\>/4/, :defaults=\>{  
:action=\>‘show’}  
map.connect ‘:controller/:action’, :defaults=\>{:action=\>‘index’}

and when I point my browser to “localhost:3000/departments” I see an  
error because the show action cannot find a department with id=0. Why  
is the first route the one that is being used. I should be the second  
and I should see the index action’s results.

Really what I want is :id=\>/^\d+.\*/ but I can’t even get the above  
example working.

Any ideas?

Thanks,  
Peter

---

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [July 15, 2006, 8:35pm UTC](https://www.ruby-forum.com/t/routing-requirements-broken-in-rails-1-1-4/98509/2 "2006-07-15T20:35:12Z")

</div>

Anyone? I’m totally stumped on this one.

Thanks,  
Peter

---

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [July 16, 2006, 8:08am UTC](https://www.ruby-forum.com/t/routing-requirements-broken-in-rails-1-1-4/98509/3 "2006-07-16T08:08:16Z")

</div>

Hi,

I created a fresh rails app with 1.1.4 to try and solve my problem

$ rails temp  
$ cd temp  
$ script/generate controller front index show

I edited the routes.rb file to have only two routes

map.connect ‘:controller/:id/:action’, :id=\>/4/, :defaults=\>{  
:action=\>‘show’}  
map.connect ‘:controller/:action’, :defaults=\>{:action=\>‘index’}

When I type [http://localhost:3000/front](http://localhost:3000/front) into my browser I see the show  
template. I thought I should see the index template.

Is this expected behavior?

Thanks,  
Peter

---

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [July 16, 2006, 12:27pm UTC](https://www.ruby-forum.com/t/routing-requirements-broken-in-rails-1-1-4/98509/4 "2006-07-16T12:27:49Z")

</div>

\<…\>

> I edited the routes.rb file to have only two routes
> 
> map.connect ‘:controller/:id/:action’, :id=\>/4/, :defaults=\>{ :action=\>‘show’}  
> map.connect ‘:controller/:action’, :defaults=\>{:action=\>‘index’}
> 
> When I type [http://localhost:3000/front](http://localhost:3000/front) into my browser I see the show  
> template. I thought I should see the index template.
> 
> Is this expected behavior?

Yes. :name =\> /regexp/ or :requirements =\> { :name =\> /regexp/ } work  
only on parameters provided in URL, so if you call  
[http://localhost:3000/front](http://localhost:3000/front) first rule in you routes matches,  
assuming :id =\> nil and :action =\> show. The :id=\>/4/ part is not used.  
Try [http://localhost:3000/front/5/](http://localhost:3000/front/5/) and you will get error - because  
now :id is present in URL and it does not match /4/.

Maybe you want something like this:

map.connect ‘:controller/:action’, :action =\> /\D+/  
map.connect ‘:controller/:id/:action’, :defaults =\>{:action =\>  
‘show’}, :id =\> /\d+/

:defaults =\> { :action =\> ‘index’ } is not necessary, because it is  
default by default 🙂

## Regards, Rimantas

[http://rimantas.com/](http://rimantas.com/)

---

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [July 16, 2006, 7:44pm UTC](https://www.ruby-forum.com/t/routing-requirements-broken-in-rails-1-1-4/98509/5 "2006-07-16T19:44:10Z")

</div>

Hi Rimantas!

On 7/16/06, Rimantas L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Yes. :name =\> /regexp/ or :requirements =\> { :name =\> /regexp/ } work  
> only on parameters provided in URL, so if you call  
> [http://localhost:3000/front](http://localhost:3000/front) first rule in you routes matches,  
> assuming :id =\> nil and :action =\> show. The :id=\>/4/ part is not used.

That is absolutely bizarre behavior to me. I made a point of saying  
that id must match ‘4’ but if I don’t include a 4 in the URL then it  
works ok as nil. Imagine that policy for a username/password form. The  
password must be “abracadabra” but nil is ok too.

Anyway, thanks for the information. This has been driving me nuts.

Peter
