views:

385

answers:

5

Routes in Ruby on Rails are case sensitive. It seems someone brought this up before, and it has been labeled will not fix.

http://rails.lighthouseapp.com/projects/8994/tickets/393-routes-are-case-sensitive

That strikes me as unfortunate, as I don't really see any upside on my own application for routes to be case sensitive, while on the downside it creates a potential for confusion and a general appearance of lack of polish in my opinion.

What's the best way to make my routes case insensitive?

I found this tip on a Google search:

map.connect "web_feeds/:action", :controller  => 'web_feeds', :action => /[a-z_]+/i

This is clever, but it still leaves the web_feeds portion of the url case sensitive. I don't see any similar way around this, however, without entering in each possible combination of wEb_feEds manually, but that is obviously a horrible solution for any number of reasons.

+4  A: 

Routes in Rails are case sensitive because URLs are case sensitive. From the W3C:

URLs in general are case-sensitive (with the exception of machine names). There may be URLs, or parts of URLs, where case doesn't matter, but identifying these may not be easy. Users should always consider that URLs are case-sensitive.

John Topley
Interesting, did not know that.
Davy8
I think that if the Web had started out on Windows then it might not have turned out that way!
John Topley
I appreciate the fact that there are times when it is important to be able to differentiate case (though frankly the only instances I can think of are corner cases), but for the ordinary usage, this is against user expectations. Users expect stackoverflow.com/questions to be the same as stackoverflow.com/Questions, and indeed, this is the case. And I'm not sure ordinary users are not going to be taking the advice of the W3C that they "should always consider that URLs are case sensitive" into consideration.
WIlliam Jones
+3  A: 

Well you could try another approach. Make the case transform serverside and send everything to rails downcase.

I think you can achieve this with either mod_rewrite or mod_spelling.

vise
A: 

Though URLs are case-sensitive, if you want to make your routes case-insensitive, there is a dirty hack you can do.

In application_controller.rb put:

rescue_from ActionController::RoutingError do |exception|
 redirect_to request.url.downcase
end

But don't actually do that. You create a redirect loop for any non-existant routes. You really should parse request.request_uri into its components, downcase them, and use them to generate the legit route that you redirect to. As I mentioned right off, this is a dirty hack. However, I think this is better than making your route map ugly and hackish.

Dom Brezinski
You should check if it's already downcased, something likeif request.url != request.url.downcaseredirect_to request.url.downcaseelse# render 404 fileend
vise
+3  A: 

I've just has the same problem, and solved it using middelware - have a look here:

http://gehling.dk/2010/02/how-to-make-rails-routing-case-insensitive/

Note: This only applies for Rails 2.3+

  • Carsten
Carsten Gehling
A: 

yeah, pratik's a douchebag. just monkey-patch it to downcase by default. stupid simple example:

module ActionController
  module Caching
    module Pages
      def cache_page(content = nil, options = nil)
        return unless perform_caching && caching_allowed

        path = case options
          when Hash
            url_for(options.merge(:only_path => true, :skip_relative_url_root => true, :format => params[:format]))
          when String
            options
          else
            request.path
        end

        path = path.downcase

        self.class.cache_page(content || response.body, path)
      end
    end
  end
end
bitemerailsboys
As the W3C states, URL's are case-sensitive
Neil Middleton