Ruby on rails 如何在Rails URL中使用UTF?
我在Ruby on rails 如何在Rails URL中使用UTF?,ruby-on-rails,url,utf-8,internationalization,Ruby On Rails,Url,Utf 8,Internationalization,我在routes.rb中有以下路线: map.resources 'protégés', :controller => 'Proteges', :only => [:index] # # this version doesn't work any better: # map.resources 'proteges', :as => 'protégés', :only => [:index] 当我进入“http://localhost:3000/protégés“我得到以
routes.rb中有以下路线:
map.resources 'protégés', :controller => 'Proteges', :only => [:index]
#
# this version doesn't work any better:
# map.resources 'proteges', :as => 'protégés', :only => [:index]
当我进入“http://localhost:3000/protégés
“我得到以下信息:
No route matches "/prot%C3%A9g%C3%A9s" with {:method=>:get}
我认为我使用的HTTP服务器(Mongrel)没有正确地避开。我还尝试了乘客的Apache,但没有成功。我尝试添加机架中间件:
require 'cgi'
class UtfUrlMiddleware
def initialize(app)
@app = app
end
def call(env)
request = Rack::Request.new(env)
puts "before: #{request.path_info}"
if request.path_info =~ /%[0-9a-fA-F]/
request.path_info = CGI.unescape(request.path_info)
end
puts "after: #{request.path_info}"
@app.call(env)
end
end
我在日志中看到了正确的信息:
before: /prot%C3%A9g%C3%A9s
after: /protégés
但我仍然看到相同的“无路由匹配”错误
我如何说服Rails使用国际化路线?我使用Rails 2.3.5是为了它的价值。问题是Rails使用了“REQUEST\u URI”
环境变量。因此,以下工作:
# in UtfUrlMiddleware:
def call(env)
if env['REQUEST_URI'] =~ /%[0-9a-fA-F]/
env['REQUEST_URI'] = CGI.unescape(env['REQUEST_URI'])
end
@app.call(env)
end