2012-02-10 57 views
0

問題我升級我的應用程序軌道3,我有點困惑的一些路線。足智多謀的人是很容易的,但我怎麼可以設置在特定的控制器的所有動作的通用規則。我想是這樣的:約Rails3中路線

get 'custom/:action/' => {:controller => :custom} 

但沒有奏效。看來新的格式是「控制器#行動」,但我怎麼可以指定動作要變?

而且,比使用命名路由或其他資源,是有可能做速記符號來命名的路線在特定的控制器?

即而不是:

get '/tasks', :controller => :home, :action => :tasks, :as => 'tasks_home' 
get '/accounts', :controller => :home, :action => :accounts, :as => 'accounts_home' 

是有可能做的東西有點清潔劑,如:

controller => :home do 
    get :tasks 
    get :accounts 
end 

這將自動創建命名的路線?

+0

如果它是一個資源,那麼你可以做 '資源:帖子不 成員做 得到「假」, :as =>'dummy_home' get'dummy1',:as =>'dummy1_home' end end ' – 2012-02-10 10:29:50

回答

0

您可以用行動像這樣的變量:

resource :custom do 
    match ':action' 
end 

這將產生

    /custom/:action(.:format) customs#:action 
    custom POST /custom(.:format)   customs#create 
new_custom GET /custom/new(.:format)  customs#new 
edit_custom GET /custom/edit(.:format) customs#edit 
      GET /custom(.:format)   customs#show 
      PUT /custom(.:format)   customs#update 
      DELETE /custom(.:format)   customs#destroy 

所以它會處理你這種行爲是可變的URL-S並會增加一些默認的CRUD操作以及。

請注意,此處的控制器名稱是複數形式。如果你想使用一個控制器,它的名字是單數的路由,使用resources代替resource

回答第二個問題是幾乎相同的第一個,使用資源:

resource :home do 
    get :tasks 
    get :accounts 
end 

產生:

tasks_home GET /home/tasks(.:format)  homes#tasks 
accounts_home GET /home/accounts(.:format) homes#accounts 
     home POST /home(.:format)   homes#create 
    new_home GET /home/new(.:format)  homes#new 
    edit_home GET /home/edit(.:format)  homes#edit 
       GET /home(.:format)   homes#show 
       PUT /home(.:format)   homes#update 
       DELETE /home(.:format)   homes#destroy 

注意匹配的控制器名稱是複數再次,由於公約。

+1

謝謝...這實際上是我在rails2中做的,但感覺像是一個黑客,因爲我沒有使用任何足智多謀的路線。從本質上講,我添加了這樣的東西來禁用那些在這些控制器中沒有端點的寧靜路線: resource:home,:except => [:new,:create,:edit,:destroy] do ...結束 – gmoniey 2012-02-10 18:01:42