Ruby on Rails Better Wiki URLs
Posted by Max Dunn Sat, 16 Sep 2006 18:36:00 GMT
Normally links in a wiki would appear like this:
http://www.maxdunn.com/show/Ruby+on+Rails
However, I wanted the URLs in my wiki to look more like web pages:
http://www.maxdunn.com/Ruby+on+Rails
It is simple to do this and here are the details:
To make your wiki URLS look more like a normal web page, rewrite the routing table in routes.rb to something like this:
DEFAULT_WEB = 'wiki' unless defined?(DEFAULT_WEB)
ActionController::Routing::Routes.draw do |map|
map.connect 'rss_with_headlines', :controller => 'wiki', :web => DEFAULT_WEB, :action => 'rss_with_headlines'
map.connect 'rss_with_content', :controller => 'wiki', :web => DEFAULT_WEB, :action => 'rss_with_content'
map.connect '', :controller => 'wiki', :web => DEFAULT_WEB, :action => 'show', :id => 'HomePage'
map.connect 'index.htm', :controller => 'wiki', :web => DEFAULT_WEB, :action => 'show', :id => 'HomePage'
map.connect ':id', :controller => 'wiki', :web => DEFAULT_WEB, :action => 'show'
map.connect '_edit/:id', :controller => 'wiki', :web => DEFAULT_WEB, :action => 'edit'
map.connect '_action/:controller/:action/:id', :web => DEFAULT_WEB
end
The key to this is that all actions that don’t just show a page start with an underscore ”_” so users can still name pages anything they want – as long as they don’t start with an underscore. Any other actions will get a long url like:
http://www.maxdunn.com/_action/wiki/revisions/Ruby+on+Rails?rev=2
However, we can still map special actions to shorter URLs, like we did for “edit”
http://www.maxdunn.com/_edit/Ruby+on+Rails
Note that “rss_with_headlines” has a special mapping. This is because Google SiteMaps requires that you have your rss feed at the top level.