# Optional Separator in Routes

**URL:** <https://www.ruby-forum.com/t/optional-separator-in-routes/116537>\
**Category:** Rails\
**Created:** [October 7, 2007, 2:56am UTC](https://www.ruby-forum.com/t/optional-separator-in-routes/116537 "2007-10-07T02:56:15Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jason\_Wentworth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d26b3c/32.png) [@Jason\_Wentworth](https://www.ruby-forum.com/u/Jason_Wentworth)\
**Post date:** [October 7, 2007, 2:56am UTC](https://www.ruby-forum.com/t/optional-separator-in-routes/116537/1 "2007-10-07T02:56:15Z")

</div>

So I’m playing with creating some custom routes, and I want to be able  
to  
pass in an optional format parameter to specify what should be returned.

Here is my current mapping:

map.blog\_statements “blogs/:id/comments.:format”,  
:conditions =\> { :method =\> :get },  
:controller =\> “blogs”,  
:action =\> “list\_comments”,  
:id =\> /\d+/,  
:format =\> nil

which works, except each time it enforces that the . be present, so  
blogs/1/comments doesn’t work but blogs/1/comments. does work.

What I find interesting however is that if I switch the . to a / so the  
route is:

map.blog\_statements “blogs/:id/comments/:format”,  
:conditions =\> { :method =\> :get },  
:controller =\> “blogs”,  
:action =\> “list\_comments”,  
:id =\> /\d+/,  
:format =\> nil

Then it works, and will accept either blogs/1/comments or  
blogs/1/comments/xml . However doing it with the / conflicts with some  
of  
my following routes so I’d really like to be able to have an optional  
:format parameter, and if it’s not present to not enforce the . be  
present.

Currently my work around involves basically listing the route twice, one  
without the format parameter at all, and then one with the format  
parameter,  
that way the first route catches it if there isn’t a period. However,  
this  
doesn’t seem the most graceful of solutions so I was wondering if I was  
just  
doing some bonehead move and missing something obvious, or if someone  
else  
had encountered this before.
