Reputation: 1810
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
Upvotes: 3
Views: 1678
Reputation: 18845
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
Upvotes: 9