2011-05-30 76 views
2

我有一個簡單的路線,和一個簡單的主菜單:使用Rails 3 root_path和root_to與區域設置URL路徑

的routes.rb

scope "(:locale)", :locale => /en|nl/ do 
    resources :posts, :only => [:show, :index] 
end 
root :to => "posts#index" 

菜單

%ul#nav-language 
    %li.alpha 
    = menu_link_to(t('app.english'), root_path) 
    %li.omega 
    = menu_link_to(t('app.nederlands'), "/nl/posts") 
%ul#nav-section 
    %li 
    = menu_link_to(t('app.blog'), {:controller => "posts", :action => "index") 
    -#more %li's. 

menu_link_to是一個圍繞link_to的簡單包裝,當選擇當前菜單時,除了設置「主動」類別外:

def menu_link_to(title, options = {}, html_options = {}) 
    if current_page?(options) 
     html_options[:class] ||= [] 
     html_options[:class] << "active" 
    end 

    link_to(title, options, html_options) 
    end 

正如您所看到的,我有兩個指向根的菜單項。在一個更直觀的方式,我的菜單看起來像:

  • EN - >/
    • 博客 - >/
    • 關於等
  • NL - >/NL /職位
    • 博客 - >/NL /職位。在菜單中,這是相同的條目爲英語,只能用不同的語言環境的前綴。

我真的不介意的EN和»博客鏈接到/ EN /職位,如果簡化的東西。

但是現在,當訪問根路徑時,current_path()將返回FALSE。這也表現奇怪,在這個意義上軌將嘗試設置的地點作爲名稱=值PARAM:/?locale=en

我想我錯過了一些重要的東西root root或路由。有任何想法嗎?

編輯:澄清了 「博客」 項目。

回答

2

您是否嘗試過在你的routes.rb文件中添加匹配?右前根:

match '/:locale' => 'dashboard#index'

它在Rails Internationalization (I18n) API

被提及或者你可以把根也進入範圍是這樣的:

scope "(:locale)", :locale => /en|nl/ do 
    resources :posts, :only => [:show, :index] 
    root :to => "posts#index" 
end 

然後,如果你不指定區域,默認的語言環境將被使用。