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
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
StackOverflow ignores the string second parameter at request processing time. At URL construction time it adds it for humans and (probably) SEO.
I'd start by looking at the capabilities of something like friendly_id and see if those aren't what you're looking for...
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).
map.resources :questions
map.friendly 'questions/:id/:title', :controller => 'questions', :action => 'show'
<%= link_to 'Show', friendly_path(question.id, question.title.parameterize) %>
These are my final customizations. Any better ideas ?