Alex Rivera | Logout

Creating routes with an optional path prefix

Asked 2008-10-17T14:39:23.900
23

How can I go about making my routes recognise an optional prefix parameter as follows:

/*lang/controller/id

In that the lang part is optional, and has a default value if it's not specified in the URL:

/en/posts/1   => lang = en
/fr/posts/1   => lang = fr
/posts/1      => lang = en

EDIT

Ideally, I'm looking to do this across many controllers and actions by mapping a namespace:

map.namespace "*lang" do |lang|
  lang.resources :posts
  lang.resources :stories
end
Edit
Report

1 Answer

2

You can define defaults in a route using the :defaults key. Try this:

map.connect ':lang/posts/:id', :controller => 'posts', :action => 'show',
                               :defaults => { :lang => 'en' }
answered 2008-10-17T17:42:13.360

Your Answer