如果路由匹配正则表达式,如何在 rails 中重定向?

How to redirect in rails if route matches regex?

我在 routes.rb

有两条路线
get 'schools/*criteria', to: 'schools#show'
// need this route to redirect to above route
get 'schools/*criteria/school-[a-zA-z]' ,to: redirect('schools/*criteria', status: 301)

例如:当我尝试这个 url /schools/elementary/school-c 它必须重定向到 /schools/elementary.但它没有发生。

任何帮助将不胜感激。

您可以尝试使用 constraint 并将 title 传递给参数

get 'schools/*criteria/:title', constraint: { title: /school-[a-zA-z]/ } ,to: redirect('schools/*criteria/%{title}', status: 301)