如何在 rails 中的路由之前从 url 中去除空格

How to strip whitespace from url before routes in rails

我们在 rails 应用程序中针对带空格的网址发出回调。

例题URL:

www.example.net/my_collection/my_endpoint%20

路由配置:

resources :my_collection do
  collection do
    post :my_endpoint
  end
end

这会导致请求与 my_endpoint 的路由不匹配。是否有任何我可以更改的设置或我可以编写的自定义代码来使请求匹配,就好像没有空格一样?

您可以使用 route globbing 捕获(并可能忽略)部分传入路径

resources :my_collection do
  collection do
    post '/endpoint(*junk)' => 'endpoints#create'
  end
end