2010-05-27 68 views
1

我嘗試了Rails,並且發現了我的路由問題。Rails 3路由 - 什麼是最佳實踐?

我有一個名爲「Account」(單數)的控制器,它應該處理當前登錄用戶的各種設置。

class AccountController < ApplicationController 
    def index 
    end 

    def settings 
    end 

    def email_settings 
    end 
end 

我該如何設置適當的路線?目前,我有:

match 'account(/:action)', :to => 'account', :as => 'account' 

然而,這並不自動地產生像account_settings_path方法,但只有account_path

是否有這樣做的更好的做法?請記住,Account控制器不代表ActiveModel的控制器。

如果這實際上是最佳實踐,我將如何在我的觀點中爲行爲生成鏈接? url_to :controller => :account, :action => :email_settings

謝謝!

回答

3

要獲得要在您的視圖中使用的命名URL,您需要指定要在routes.rb中命名的每個路由。

match 'account', :to => 'account#index' 
match 'account/settings', :to => 'account#settings' 
match 'account/email_settings', :to => 'account#email_settings' 

或者

scope :account, :path => 'account', :name_prefix => :account do 
    match '', :to => :index, :as => :index 
    match 'settings', :to => :settings 
    match 'email_settings', :to => :email_settings 
end 

無論是作品一樣,它只是一個選擇的問題。但我認爲第一種方法即使不是乾的,也是最乾淨的。

+0

此外,您不必使用':to',但也可以這樣做:'match'account'=>'account#index'' – Kris 2011-05-19 10:23:59

0

你也可以把它定義爲對資源的集合:

resources :login do 
    collection { get :reminder, :test } 
    end 

當然,這也定義了默認的CRUD操作爲好。我目前只使用其中的兩個用於我的非實際模型控制器,但我不認爲額外路線會有任何問題。

+0

這會增加很多額外的路線,毫無用處 – shingara 2010-06-25 07:06:23