Necessary and additional parameters for the Sinatra route

Using Sinatra routes, there can be either a required named parameter or an optional named parameter in the same part of the route.

The optional route parameter works fine here

get '/widgets.?:format?'

But try to combine the required named parameter and everything will break.

get '/widgets/:id.?:format?'

Requests for /widgets/abc.json pass the entire abc.json file as an id parameter.

Sinatra compiled regex:

/^\/widgets\/([^\/?#]+)(?:\.|%2E)?([^\/?#]+)?$/
+5
source share
1 answer

I skipped this by switching to full regex on the route and excluding ".". from the first group of regular expressions.

get %r{/widgets\/([^\/?#\.]+)(?:\.|%2E)?([^\/?#]+)?}
+2
source