0

I have this in my routes:

resources :users do
  resources :posts
end

It creates the show path /users/:user_id/posts/:post_id

Great. But if someone types /users/:user_id/post/:post_id (mind the singular /post and not /posts) then they get a 404. I want to do a 302 redirect from .../post/:post_id to ../posts/:post_id in my routes. How?

Ben
  • 2,957
  • 2
  • 27
  • 55

1 Answers1

4

You may be able to achieve that by using the following in your routes config.

get '/users/:user_id/post/:post_id', to: redirect('/users/%{user_id}/posts/%{post_id}')

More info here: http://guides.rubyonrails.org/routing.html Section: 3.12 Redirection

torresomar
  • 2,219
  • 2
  • 16
  • 28
  • Yup. That'll do. You do need to specify the status in the redirect and you shouldn't have a space between redirect and the opening parenthesis. Thanks. – Ben Jun 01 '16 at 21:09
  • Yup, sorry that was a typo. If you need more info regarding the redirect options you can see the source in http://api.rubyonrails.org/classes/ActionDispatch/Routing/Redirection.html – torresomar Jun 01 '16 at 21:15