3

I have a resource 'Post' in my app. The default routes for resources :posts gives urls like /posts/:id. Is it possible to remove 'posts' from the route, and just have /:id instead?

2 Answers 2

7

You can use :path to remove the '/posts' bit...

resources :posts, :path => "/"

Just be aware that this could confuse other routes which are defined below it in your routes file. It's best to have this kind of catch-all route at the bottom for this reason.

Sign up to request clarification or add additional context in comments.

1 Comment

Ah, I just found answers saying 'path_prefix' => '/'. I guess it was changed to 'path' in Rails 3. Thanks!
2

You can add a custom route for this (make sure to put it at the bottom of your routes.rb file, otherwise it'll match non-posts routes too)

match ':id' => 'posts#show'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.