Sanchez
Sanchez

Reputation: 13

restful route customization

How can i customize restful route urls to friendly urls like stackoverflow ?

i have stackoverflow.com/questions/424/

and i want to have stackoverflow.com/questions/424/title-of-the-page

Upvotes: 0

Views: 121

Answers (4)

Sanchez
Sanchez

Reputation: 13

map.resources :questions
map.friendly 'questions/:id/:title', :controller => 'questions', :action => 'show'

These are my final customizations. Any better ideas ?

Upvotes: 1

John Hyland
John Hyland

Reputation: 6872

If there's only one controller you want to do this with, the simplest solution would probably be to add a route with an ignored parameter, like so:

# config/routes.rb
map.connect 'questions/:id/:ignored', :controller => 'questions', :action => 'show'

Make sure you put this before the default routes at the bottom of that file. Or, even better, comment them out if you're using named routes and resources (as suggested in the auto-generated comments).

Upvotes: 0

I'd start by looking at the capabilities of something like friendly_id and see if those aren't what you're looking for...

Upvotes: 0

Ari Pernick
Ari Pernick

Reputation: 479

StackOverflow ignores the string second parameter at request processing time. At URL construction time it adds it for humans and (probably) SEO.

Upvotes: 0

Related Questions