在Rails中使用I18n时重定向将正斜杠编码为%2F(Redirect when using I18n with Rails is encoding the forward slash as %2F

编程入门 行业动态 更新时间:2024-10-28 16:26:12
在Rails中使用I18n时重定向将正斜杠编码为%2F(Redirect when using I18n with Rails is encoding the forward slash as %2F)

我最近在跟踪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

更多推荐

本文发布于:2023-07-25 10:34:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1259964.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:斜杠   重定向   I18n   Rails   encoding

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!