0

「レールキャスト」のキャストを見て、彼らが私に言ったようにしました。これが私の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"

私はそれを取り除きたい。私は何をすべきか?

4

2 に答える 2

0

問題はルート ファイルにあります (他の場所にリダイレクトしないと仮定します)。最後のビットを次のように変更してみてください。

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

そしておそらく他の場所で呼び出しを削除して

puts "INFO...

ログが乱雑になっています。

于 2012-08-31T07:20:34.383 に答える
0

問題は、無効なリソースへのスコープ リクエストを処理していないことです。アプリは、ロケールを追加してリクエストをリダイレクトしますが、ロケール ブロックに一致するものがない場合は、下に進み、matchキャッチオール行に到達し、別のロケールを無限に追加します。

次のように、リソースを宣言した、スコープ ブロックに別のキャッチオール行を追加する必要があります。

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}")

あなたが提示したケースでは、リクエスト URL はどのリソースとも一致しません。これは、リソースを の下に名前空間化したためです。そのため、接頭辞などadminを持つ URL でそれらにアクセスする必要があります。/en/admin//es/admin/

こちらもご覧ください:なぜ Rails アプリはルートに一致するのではなく予期せずリダイレクトするのですか?

于 2012-08-31T07:21:01.420 に答える