Pregunta

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

¿Fue útil?

Solución

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

Otros consejos

Just make the scope optional adding parenthesis, "(:locale)", so you don't need to match or redirect anything

scope "(:locale)", locale: /#{I18n.available_locales.join("|")}/ do
  resources animals
end
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top