Search code examples
ruby-on-railsroutesrails-routing

How to redirect in rails if route matches regex?


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.


Solution

  • You can try using constraint and pass title to params

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