match in rails routes.rb is redirecting too many t

2020-08-01 02:02发布

问题:

I have a route that is suppose to match something like this
localhost:3000/site/admin

and redirect to

localhost:3000/en/site/admin

here is the route line that should match this
routes.rb

match '*path', to: redirect("/#{I18n.default_locale}/%{path}")

but instead of matching it and redirecting to localhost:3000/en/site/admin, It's redirecting infite to this

http://localhost:3000/en/en/en/en/en/en/en/en/en/en/en/en/en/site/admin
(adds /en until browser complains about infite loop)

Any idea why this infite loops is happening? Thanks

EDIT

Link to my my entire routes.rb file

https://gist.github.com/2323762

回答1:

Do you have a route higher up in the file that catches the "/:locale/:path" URL that you're redirecting to? If not, your route will fall through and be caught once again by the "*path" route (which matches anything).