Rails Routes - How to make them case insensitive?

后端 未结 6 473
春和景丽
春和景丽 2021-01-17 18:10

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/89

相关标签:
6条回答
  • 2021-01-17 18:14

    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.

    0 讨论(0)
  • 2021-01-17 18:15

    just monkey-patch it to downcase by default. 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
    
    0 讨论(0)
  • 2021-01-17 18:18

    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.

    0 讨论(0)
  • 2021-01-17 18:25

    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.

    0 讨论(0)
  • 2021-01-17 18:29

    A simple solution is, may be not an elegant way, but yet workable is: Use a before_filter in your application controller like this.

      before_filter :validate_case
    
      def validate_case
        if request.url != request.url.downcase
          redirect_301_permanent_to request.url.downcase
        end
      end
    
      def redirect_301_permanent_to(url)
         redirect_to url, :status=>:moved_permanently 
      end
    

    As i already told that its not an elegant but yet workable, so no down votes please. :P

    0 讨论(0)
  • 2021-01-17 18:35

    I've just has the same problem, and solved it using middleware - 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
    0 讨论(0)
提交回复
热议问题