Optional Separator in Routes

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs