# Routes Optional Values

**URL:** <https://www.ruby-forum.com/t/routes-optional-values/87191>\
**Category:** Rails\
**Created:** [February 28, 2007, 7:56pm UTC](https://www.ruby-forum.com/t/routes-optional-values/87191 "2007-02-28T19:56:23Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![divotdave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bbe5ce/32.png) [@divotdave](https://www.ruby-forum.com/u/divotdave)\
**Post date:** [February 28, 2007, 7:56pm UTC](https://www.ruby-forum.com/t/routes-optional-values/87191/1 "2007-02-28T19:56:23Z")

</div>

I’m trying to my a url that has optional (and not optional values)

for example

[http://myserver/store/cats/a/b/c](http://myserver/store/cats/a/b/c)

A and C are optional  
where the following are valid

[http://myserver/store/cats/a/b/c](http://myserver/store/cats/a/b/c)  
[http://myserver/store/cats//b/c](http://myserver/store/cats//b/c)  
[http://myserver/store/cats/a/b](http://myserver/store/cats/a/b)  
[http://myserver/store/cats//b](http://myserver/store/cats//b)

And the following is invalid

[http://myserver/store/cats/a//c](http://myserver/store/cats/a//c)

I know the last value can be optional by including ,:c=\>nil  
But the only way I can think of for middle values to be optional is to  
have all permutations written out.

for example  
map.connect ':store/:cats/:a/:b/:c, :c=\> nil  
map.connect ':store/:cats//:b/:c, :a=\> nil, :c=\> nil

For a couple of variables this is fine, but moving to 3 or more, and it  
gets nuts fast.

Is there an easier way to handle this?

---

<div class="post-metadata">

**Author:** ![divotdave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bbe5ce/32.png) [@divotdave](https://www.ruby-forum.com/u/divotdave)\
**Post date:** [February 28, 2007, 10:00pm UTC](https://www.ruby-forum.com/t/routes-optional-values/87191/2 "2007-02-28T22:00:18Z")

</div>

> [http://myserver/store/cats//b/c](http://myserver/store/cats//b/c)
> 
> for example  
> map.connect ':store/:cats/:a/:b/:c, :c=\> nil  
> map.connect ':store/:cats//:b/:c, :a=\> nil, :c=\> nil
> 
> For a couple of variables this is fine, but moving to 3 or more, and it  
> gets nuts fast.
> 
> Is there an easier way to handle this?

Dont’ know if it’s any easier, but it’s a cleaner route…

map.connect “:store/:cats/\*args”,…

Then in the action that handles the above route, you’ll have  
params[:args]  
as an array and can do whatever you want with it redirecting to an error  
(or 404, etc.) page if it isn’t what you want.

-philip
