Sinatra路线的必需和可选参数

时间:2022-03-22 08:28:41

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

使用Sinatra路由,如何在路径的同一部分中同时存在所需的命名参数和可选的命名参数。

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.

对/widgets/abc.json的请求将整个abc.json作为id参数传递。

The Sinatra compiled regex is:

Sinatra编译的正则表达式是:

/^\/widgets\/([^\/?#]+)(?:\.|%2E)?([^\/?#]+)?$/

1 个解决方案

#1


2  

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

我确实通过在路线上完整的正则表达式并排除了“。”来解决这个问题。来自第一个正则表达组。

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

#1


2  

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

我确实通过在路线上完整的正则表达式并排除了“。”来解决这个问题。来自第一个正则表达组。

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