I want to create a Rails 3 route with entirely optional parameters. The example broken route is:
match '(/name/:name)(/height/:height)(/weight/:weight)' => 'people#index'
Which results in ‘rake:routes’ yielding:
/(/name/:name)(/height/:height)(/weight/:weight)
And thus adding an initial slash to all links:
<a href="//name/kevin">...</a>
The route works if I specify it as:
match '/people(/name/:name)(/height/:height)(/weight/:weight)' => 'people#index'
But I want to have this as the root URL (as with the first example, which does not work). Thanks.
I don’t know if this can be done with Rails’ routing engine, but you can add a custom middleware to your stack and it should work just fine.
Put this in
lib/custom_router.rband add
to your
config/application.rb.You can then set the route like
and it will work.