使用带有Rails的I18n时重定向将正斜杠编码为%2F

Gar*_*nes 3 ruby-on-rails url-routing internationalization i18n-gem

我最近在I18n上关注Ryan Bate的Railscast,添加了多个位置并从URL Params设置区域设置

www.example.com/en/
www.example.com/fr/

通常它是一种享受,但我注意到,如果我手动尝试从URL中删除位置,结果重定向不能正确形成,似乎编码/进入%2F.例如,如果我从中删除网址

www.example.com/fr/animals/horses
所以它是www.example.com/animals/horses

然后重定向产生以下网址:
www.example.com/fr/animals%2Fhorses

这是我的routes.rb的一部分

scope ":locale", locale: /#{I18n.available_locales.join("|")}/ do
 resources animals
end

match '*path', to: redirect("/#{I18n.default_locale}/%{path}"), constraints: lambda { |req| !req.path.starts_with? "/#{I18n.default_locale}/" }
match '', to: redirect("/#{I18n.default_locale}")
Run Code Online (Sandbox Code Playgroud)

我试图找出一种方法将CGI :: escape放入{path},但到目前为止我没有尝试过任何方法.有谁知道正确的代码来解决这个问题?

Rails 3.2.6/Ruby 1.9.2

谢谢

pho*_*oet 9

我想你需要做的是使用一个块:

match '*path', to: redirect {|params| "/bla/#{CGI::unescape(params[:path])}" }
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请参阅指南http://guides.rubyonrails.org/routing.html#redirection

  • 完美,非常感谢!对于其他感兴趣的人,我使用了以下内容:`match'*path',to:redirect {| params | "/#{I18n.default_locale}/#{CGI::unescape(params[:path])}"},constraints:lambda {| req | !req.path.starts_with?"/#{I18n.default_locale}/"}` (4认同)