More on how to customize your routes in Rails 1.

This article is deprecated.I've marked this article as deprecated. It is either because the gem or plugin discussed in this article is no longer maintained, or I've changed my opinion on it. This article is here only as an historic reference.

Fancy Named Routes - Part II

Here are some improvements for Fancy Named Routes - Part I. In this part we will be adding a more thorough solution for the html escaping in everywhere. I noticed this was absolutely important, when I tried to view an escaped url in a production environment with Apache with mod_proxy and mongrel_cluster. Apache doesn't like '%2F' in the title, and doesn't forward the request to mongrel, so it returns a nice 404.

To rid yourself of the problem once and for all, add this to your ApplicationController:

def url_for(options)
  url = super(options)
  url.gsub!("%2F","/") if has_nice_url?(url)

def has_nice_url?(link)
  rs = ::ActionController::Routing::Routes
  segments = rs.recognize_path link
  rs.named_routes.routes.each do |key,value|
    return true if
      value.defaults.has_value?(segments[:controller]) and
      value.defaults.has_value?(segments[:action]) and
rescue ::ActionController::RoutingError
  logger.debug{"RoutingError has occured"}

Now there is no need for overloading link_to or redirected_to. Remember, you'll need this in your config/routes.rb, to make it all work:

map.show_article 'article/:id/*nice_url', :controller => 'articles', :action => 'show', :nice_url => nil

And in your model, you have to add:

def to_param'/'+self.title.gsub(/\W+/,'-').downcase+'.html'

Now you can make awesome links like this:

link_to h(@article.title), show_article_url(@article)
redirect_to show_article_url(@article)
comments powered byDisqus