A Day in the Life

rails 1系でURLの一段目の階層のみ最後に / をつける

rails 1系でURLの一段目の階層のみ最後に / をつける

#http://example.com/foo
http://example.com/foo/

#http://example.com/foo?bar=baz
http://example.com/foo/?bar=baz

みたいにしたいときのスマートな解決法が無い、と思う。 routes を弄る。

わおーBK…\(^o^)/。

class ActionController::Routing::Route
    URL_MATCH_WITH_SLASH_RE = %r{^/[^/]+$}
    def path_hook(path)
      path << '/' if path && path.match(URL_MATCH_WITH_SLASH_RE)
      path
    end

    def write_generation
      body = "expired = false\n#{generation_extraction}\n#{generation_structure}"
      body = "if #{generation_requirements}\n#{body}\nend" if generation_requirements
      args = "options, hash, expire_on = {}"
      raw_method = method_decl = "def generate_raw(#{args})\npath = begin\n#{body}\nend\n[path_hook(path), hash]\nend"
      instance_eval method_decl, "generated code (#{__FILE__}:#{__LINE__})"
      method_decl = "def generate(#{args})\npath, hash = generate_raw(options, hash, expire_on)\nappend_query_string(path, hash, extra_keys(hash, expire_on))\nend"
      instance_eval method_decl, "generated code (#{__FILE__}:#{__LINE__})"
      method_decl = "def generate_extras(#{args})\npath, hash = generate_raw(options, hash, expire_on)\n[path, extra_keys(hash, expire_on)]\nend"
      instance_eval method_decl, "generated code (#{__FILE__}:#{__LINE__})"
      raw_method
    end
end
記事の一覧 >