为什么rails应用程序意外重定向而不是匹配路由?

我之前提过这个问题并认为它已修复,但事实并非如此。 先前的问题在这里

我的问题是我试图设置我的路线,以便在我输入时

本地主机:3000 /网站/管理

应该重定向到

本地主机:3000 / EN /网站/管理

这是我的routes.rb文件

scope ":locale", locale: /#{I18n.available_locales.join("|")}/ do get "log_out" => "sessions#destroy", as: "log_out" get "log_in" => "sessions#new", as: "log_in" resources :sites, except: [:new, :edit, :index, :show, :update, :destroy, :create] do collection do get :home get :about_us get :faq get :discounts get :services get :contact_us get :admin get :posts end end resources :users resources :abouts resources :sessions resources :coupons resources :monthly_posts resources :reviews resources :categories do collection { post :sort } resources :children, :controller => :categories, :only => [:index, :new, :create, :new_subcategory] end resources :products do member do put :move_up put :move_down end end resources :faqs do collection { post :sort } end root :to => 'sites#home' match "/savesort" => 'sites#savesort' end match '', to: redirect("/#{I18n.default_locale}") match '*path', to: redirect("/#{I18n.default_locale}/%{path}") 

但截至目前,它重定向到/ en / en / en / en / en / en / en / en / en / en / sites / admin(添加en直到浏览器抱怨)。

有什么想法,它一直在添加/ en?

编辑:答案很棒,谢谢。 你能帮我诊断根路线吗?

 root to: redirect("#{/#{I18n.default_locale}") # handles / 

我知道重定向正在寻找类似的东西

 redirect("www.example.com") 

这就离开了这一部分

 #{/#{I18n.default_locale} 

#{正在使用rubys字符串插值,对吗? 我不确定那是什么。

那么我们就有了

 /#{I18n.default_locale} 

哪个也使用字符串插值并打印出I18n.default_locale的值?

希望这是有道理的,我真的很感激帮助,我正在学习很多东西。

编辑2:

我改变了这条线

 root to: redirect("#{/#{I18n.default_locale}") # handles / 

 root to: redirect("/#{I18n.default_locale}") # handles / 

但我不确定这是否正确。 现在我收到了错误

 uninitialized constant LocaleController 

我知道它从根到“locale #root”的错误,但我认为语言环境#将来自范围。

我将继续玩它,让你知道任何进展。

这是我的路线文件https://gist.github.com/2332198的新链接

我们再见面,ruevaughn。 🙂

我创建了一个测试rails应用程序,以下最小的示例适用于我:

 scope ":locale", locale: /#{I18n.available_locales.join("|")}/ do resources :sites do collection do get :admin end end root to: "locale#root" # handles /en/ match "*path", to: "locale#not_found" # handles /en/fake/path/whatever end root to: redirect("/#{I18n.default_locale}") # handles / match '*path', to: redirect("/#{I18n.default_locale}/%{path}") # handles /not-a-locale/anything 

当使用Rails 4.0.x时,重定向中的%{path}将转义%{path}中的斜杠,因此您将获得无限循环重定向到/en/en%2Fen%2Fen%2Fen...

为了防止像我这样的人正在寻找适合Rails-4的解决方案,这就是我发现没有问题的工作,即使有更复杂的路径需要重定向:

 # Redirect root to /:locale if needed root to: redirect("/#{I18n.locale}", status: 301) # Match missing locale paths to /:locale/path # handling additional formats and/or get params match '*path', to: (redirect(status: 307) do |params,request| sub_params = request.params.except :path if sub_params.size > 0 format = sub_params[:format] sub_params.except! :format if format.present? "/#{I18n.locale}/#{params[:path]}.#{format}?#{sub_params.to_query}" else "/#{I18n.locale}/#{params[:path]}?#{sub_params.to_query}" end else "/#{I18n.locale}/#{params[:path]}" end end), via: :all # Redirect to custom root if all previous matches fail match '', to: redirect("/#{I18n.locale}", status: 301), via: :all