I originally had a route defined like this :
get ':foo(/:bar)/:baz(/:qux)/:slug', to: 'application#show', constraints: { baz: /.+?\-\d+/ }, as: test
With this configuration, it works fine if I omit :bar in my path :
foo/baz-123/qux-quux/test
The controller returns the following parameters : {foo: 'foo', baz: 'baz-123', qux: 'qux-quux', slug: 'test'}
Now, the specs changed, and I need to accept alphanumeric characters in the baz constraint, so I did this :
get ':foo(/:bar)/:baz(/:qux)/:slug', to: 'application#show', constraints: { baz: /.+?\-[a-z0-9]+/ }, as: test
With this configuration, parameters mapping starts behaving differently. The same path foo/baz-123/qux-quux/test returns the following parameters in the controller :
{foo: 'foo', bar: 'baz-123', baz: 'qux-quux', slug: 'test'}
I thought that it's happening because qux-quux matches the constraint too, so I changed it to /.+?\-(?=.*\d+)[a-z0-9]+/ to ensure that the :baz parameter needs at least one digit in the second part after the -.
However, the controller still returns the same wrong mapping even if qux-quxx does not match the constraint.
How does Rails map the parameters in case of multiple optional segments ?