如何在葡萄中的一个街区中创建两条路线



我想在一个动作块中捕获2个类似的路线。在Rails5中,我可以轻松地做到这一点。我首先声明了这一点:

get ':folder/:file' => 'get#index', :file => /.*/, :folder => /.*/
get ':file' => 'get#index', :file => /.*/

这使我能够在最后一个文件名中捕获:folder和文件夹一样像a/b/c/d...:file一样。第二个还允许我仅收集文件名。和两个路线都针对相同的动作。

但是,在葡萄中,因为它被声明为块而不是通往方法定义的路线,所以我必须两次编写相同的块...

有什么方法可以在一个路由参数中同时捕获/as/many/folder/and/file.ext/file.ext?我尝试了可选的参数,要求。他们都没有工作。

我使用 :folder/:file(两次regexp)背后的原因是我可以分别抓住 :folder param和 :file param而无需手动拆分。

get ':folder/:file', requirements: { file: /.*/, folder: /.*/ } do
  # params[:folder] and params[:file]
end
get ':file', requirements: { file: /.*/ } do
  # params[:file]. [:folder is empty.]
end

^^我想让它们成为一条路线。如果存在文件夹(嵌套),则它将在文件夹中访问param,否则文件夹将为nil。

好。我通过尝试和寻找弹药来找到答案。

get '(:folder/):file', requirements: {  folder: /.*/, file: /.*/ } do

这是按预期工作的。

示例:

desc 'Create transaction'
params do
  requires :id, type: String
  requires :from_, type: String
end
post ['/:id/addresses/:from_/transactions', '/:id/transactions'] do
end

路由:

/api/v1/wallets/:id/addresses/:from_/transactions  
/api/v1/wallets/:id/transactions

最新更新