How to redirect in rails if route matches regex?

时间:2017-05-16 09:37:28

标签: ruby-on-rails routes rails-routing

I have two routes in 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)

Ex: when I try this url /schools/elementary/school-c it has to redirect to /schools/elementary. but it is not happening.

Any help will be appreciated.

1 个答案:

答案 0 :(得分:2)

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

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