I18n和Ruby on Rails中的重定向会导致一些问题

时间:2012-08-31 07:04:31

标签: internationalization ruby-on-rails-3.2

我看到了一个“轨道广播”演员,就像他们告诉我的那样。这是我的route.rb

的一部分
  scope ':locale', :locale=> /#{I18n.available_locales.join("|")}/ do
    #..........
    #..........
    namespace :admin do
      resources :products do
         get :who_bought, :on=>:member
     end
     end
  end

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

因此,现在会出现一些问题。看看我的日志。

Started GET "/products/4/who_bought.atom" for 127.0.0.1 at 2012-08-31 09:58:35 +0400
2012-08-31 09:58:35 INFO -- 

Started GET "/en/products/4/who_bought" for 127.0.0.1 at 2012-08-31 09:58:35 +0400
2012-08-31 09:58:35 INFO -- 

Started GET "/en/en/products/4/who_bought" for 127.0.0.1 at 2012-08-31 09:58:35 +0400
2012-08-31 09:58:35 INFO -- 

Started GET "/en/en/en/products/4/who_bought" for 127.0.0.1 at 2012-08-31 09:58:35 +0400
2012-08-31 09:58:35 INFO -- 

Started GET "/en/en/en/en/products/4/who_bought" for 127.0.0.1 at 2012-08-31 09:58:35 +0400
2012-08-31 09:58:35 INFO -- 

Started GET "/en/en/en/en/en/products/4/who_bought" for 127.0.0.1 at 2012-08-31 09:58:35 +0400
2012-08-31 09:58:35 INFO -- 

Started GET "/en/en/en/en/en/en/products/4/who_bought" for 127.0.0.1 at 2012-08-31 09:58:35 +0400
2012-08-31 09:58:35 INFO -- 

Started GET "/en/en/en/en/en/en/en/products/4/who_bought" for 127.0.0.1 at 2012-08-31 09:58:35 +0400 

Started GET "/assets/logo.png" for 127.0.0.1 at 2012-08-31 10:55:36 +0400
2012-08-31 10:55:36 INFO -- Served asset /logo.png - 404 Not Found (13ms)
2012-08-31 10:55:36 INFO -- 

Started GET "/assets/products/tea1.jpg" for 127.0.0.1 at 2012-08-31 10:55:36 +0400
2012-08-31 10:55:36 INFO -- Served asset /products/tea1.jpg - 404 Not Found (2ms)
2012-08-31 10:55:38 INFO -- 

Started GET "/en/assets/logo" for 127.0.0.1 at 2012-08-31 10:55:38 +0400
2012-08-31 10:55:38 INFO -- 

Started GET "/en/assets/products/tea1" for 127.0.0.1 at 2012-08-31 10:55:38 +0400
2012-08-31 10:55:38 INFO -- 

Started GET "/en/en/assets/logo" for 127.0.0.1 at 2012-08-31 10:55:38 +0400
2012-08-31 10:55:38 INFO -- 

Started GET "/en/en/assets/products/tea1" for 127.0.0.1 at 2012-08-31 10:55:38 +0400
2012-08-31 10:55:38 INFO -- 

Started GET "/en/en/en/assets/logo" for 127.0.0.1 at 2012-08-31 10:55:38 +0400
2012-08-31 10:55:38 INFO -- 

Started GET "/en/en/en/assets/products/tea1" for 127.0.0.1 at 2012-08-31 10:55:38 +0400
2012-08-31 10:55:38 INFO -- 

Started GET "/en/en/en/en/assets/logo" for 127.0.0.1 at 2012-08-31 10:55:38 +0400
2012-08-31 10:55:38 INFO -- 

Started GET "/en/en/en/en/assets/products/tea1" for 127.0.0.1 at 2012-08-31 10:55:38 +0400
2012-08-31 10:55:38 INFO -- 

Started GET "/en/en/en/en/en/assets/logo" for 127.0.0.1 at 2012-08-31 10:55:38 +0400
2012-08-31 10:55:38 INFO -- 

Started GET "/en/en/en/en/en/assets/products/tea1" for 127.0.0.1 at 2012-08-31 10:55:38 +0400
2012-08-31 10:55:38 INFO -- 

Started GET "/en/en/en/en/en/en/assets/logo" for 127.0.0.1 at 2012-08-31 10:55:38 +0400
2012-08-31 10:55:38 INFO -- 

Started GET "/en/en/en/en/en/en/assets/products/tea1" for 127.0.0.1 at 2012-08-31 10:55:38 +0400
2012-08-31 10:55:38 INFO -- 

Started GET "/en/en/en/en/en/en/en/assets/logo" for 127.0.0.1 at 2012-08-31 10:55:38 +0400
2012-08-31 10:55:38 INFO -- 

Started GET "/en/en/en/en/en/en/en/assets/products/tea1" for 127.0.0.1 at 2012-08-31 10:55:38 +0400
2012-08-31 10:55:38 INFO -- 

Started GET "/en/en/en/en/en/en/en/en/assets/logo" for 127.0.0.1 at 2012-08-31 10:55:38 +0400
2012-08-31 10:55:38 INFO -- 

如果我转到"http://localhost:3000/en/fdsfdsfdsfds",它会将我重定向到"http://localhost:3000/en/en/en/en/en/en/en/en/en/en/en/en/en/en/en/en/en/en/en/en/en/fdsfdsfdsfds"

我想摆脱它。我该怎么办?

2 个答案:

答案 0 :(得分:0)

问题在于您的路线文件(假设您没有在其他地方重定向)。尝试将最后一位更改为:

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

也许可以删除其他地方的电话

puts "INFO...

这会使你的日志变得混乱。

答案 1 :(得分:0)

问题是您没有处理对无效资源的作用域请求。您的应用程序通过向其添加区域设置来重定向请求,但如果在区域设置块中没有任何内容与之匹配,则它会继续向下并到达match catch-all行,向其添加另一个区域设置,无限制。

您需要在声明资源之后在范围块中添加另一个全部

scope ':locale', :locale=> /#{I18n.available_locales.join("|")}/ do

  ...

  namespace :admin do
    resources :products do
      get :who_bought, :on=>:member
   end
  end

  root to: "locale#root"
  match "*path", to: "locale#not_found"
end

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

在您提交的案例中,请求网址与任何资源都不匹配,因为您已将资源命名为admin,因此您需要在前缀为/en/admin/的网址上对其进行访问,/es/admin/等。

另请参阅:Why rails app is redirecting unexpectedly instead of matching the route?