Optional Separator in Routes

Jason Wentworth
Sun, 07 Oct 2007 00:55:28 -0000

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.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---