我最近在跟踪Ryan Bate的Railscast on I18n中添加了多个位置,并从URL Params中设置了区域设置
www.example.com/en/ www.example.com/fr/
通常它是一种享受,但我注意到,如果我手动尝试从url中移除位置,则生成的重定向无法正确形成,看起来编码为%2F。 例如,如果我从中删除网址
www.example.com/fr/animals/horses 所以它是www.example.com/animals/horses
那么重定向会产生以下url: 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}")我试图找出一种将CGI :: escape合并到{path}中的方法,但是迄今为止我没有尝试过。 有谁知道正确的代码来解决这个问题?
Rails 3.2.6 / Ruby 1.9.2
谢谢
I was recently following Ryan Bate's Railscast on I18n adding multiple locations and setting the locale from the URL Params like
www.example.com/en/ www.example.com/fr/
Generally it works a treat, however I've noticed that if I manually try to remove the location from the url, the resulting redirect doesn't form correctly, seemingly encoding the / into %2F. For example if I remove the url from
www.example.com/fr/animals/horses so it's www.example.com/animals/horses
then the redirect produces the following url: www.example.com/fr/animals%2Fhorses
here's part of my 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}")I was trying to figure out a way of fitting CGI::escape into the {path} but nothing I've tried so far has worked. Does anyone know the correct code to fix this problem?
Rails 3.2.6 / Ruby 1.9.2
Thanks
最满意答案
我想你需要做的是使用块:
match '*path', to: redirect {|params| "/bla/#{CGI::unescape(params[:path])}" }有关更多信息http://guides.rubyonrails.org/routing.html#redirection的指南
i guess what you need to do is using a block:
match '*path', to: redirect {|params| "/bla/#{CGI::unescape(params[:path])}" }have a look at the guides for more info http://guides.rubyonrails.org/routing.html#redirection
更多推荐
发布评论