2012-04-07 30 views
3

我之前問過這個問題,並認爲它是固定的,但事實並非如此。前一個問題here爲什麼rails應用程序意外重定向而不是匹配路線?

我的問題是我試圖把我的路線,這樣,當我在

本地主機:3000 /網站/管理

應該重定向到

本地主機: 3000/en/sites/admin

這是我的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/cn/sites/admin(直到瀏覽器投訴爲止)。

任何想法爲什麼它不斷添加/ 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 

我知道它越來越錯誤從根到:「區域#根」,但我認爲在區域#將來自範圍。

我會繼續玩它,讓你知道任何進展。

這裏是一個新的鏈接到我的路線文件https://gist.github.com/2332198

回答

10

我們再次見面,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 
+0

偉大的回答和努力,+1 – apneadiving 2012-04-07 17:27:52

+1

我很高興我們再次見面:)答案比我想象的還要多。我不知道你可以有一個根:在你的範圍內,在它之外,我不知道我可以像這樣調用locale#root。我仍在學習。根源:重定向我不太確定,所以如果你有時間,我在問題中發佈了一個編輯。再次感謝 – ruevaughn 2012-04-07 21:05:04

+0

'locale#root'是'controller#action'的一個簡短形式,所以這就是LocaleController的'root'方法。我用它作爲佔位符;你可以用你的應用中的任何有意義的東西替換它。 – 2012-04-07 21:33:58

0

當使用Rails 4.0.x的是%{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 
+0

即使用您的舊代碼替換後,我仍然遇到問題。請告知 – Sai 2015-05-19 13:35:51

+0

我已經用我目前在生產環境中使用的代碼更新了我的答案。現在從未遇到大約一年的問題。讓我知道這是否適合你@Sai! – marzapower 2015-05-20 14:29:18

+1

謝謝,那對我有用。我遇到了資產預編譯問題,我認爲這也是由此引起的 – Sai 2015-05-21 15:28:15

相關問題