Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Required and optional parameters for Sinatra route

With Sinatra routes, how can there be both a required named parameter and an optional named parameter in the same part of the route.

Optional route parameter works fine here

get '/widgets.?:format?'

But, try to combine a required named paramter, and things break.

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

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

The Sinatra compiled regex is:

/^\/widgets\/([^\/?#]+)(?:\.|%2E)?([^\/?#]+)?$/
like image 624
RyanW Avatar asked Nov 05 '22 03:11

RyanW


1 Answers

I did get past this by going full regex on the route and excluding the "." from the first regex group.

get %r{/widgets\/([^\/?#\.]+)(?:\.|%2E)?([^\/?#]+)?}
like image 146
RyanW Avatar answered Nov 15 '22 08:11

RyanW