2014年8月6日 星期三

[Rails] 將 route 拆成不同個檔案.

來源:split route namespaces into different files
把 rails route file 拆成不同檔案.

之前:
# config/routes.rb ActionController::Routing::Routes.draw do |map| map.resources :posts map.resources :comments map.logout '/logout', :controller => 'sessions', :action => 'destroy' map.login '/login', :controller => 'sessions', :action => 'create' map.namespace :developer do |dev| dev.resources :posts dev.resources :comments dev.logout '/logout', :controller => 'sessions', :action => 'destroy' dev.login '/login', :controller => 'sessions', :action => 'create' end map.namespace :admin do |admin| admin.resources :posts admin.resources :comments admin.logout '/logout', :controller => 'sessions', :action => 'destroy' admin.login '/login', :controller => 'sessions', :action => 'create' end map.namespace :api do |api| api.resources :posts api.resources :comments api.logout '/logout', :controller => 'sessions', :action => 'destroy' api.login '/login', :controller => 'sessions', :action => 'create' end end
之後:
# config/routes.rb ActionController::Routing::Routes.draw do |map| map.resources :posts map.resources :comments map.logout '/logout', :controller => 'sessions', :action => 'destroy' map.login '/login', :controller => 'sessions', :action => 'create' end # config/routes/developer.rb ActionController::Routing::Routes.draw do |map| map.namespace :developer do |dev| dev.resources :posts dev.resources :comments dev.logout '/logout', :controller => 'sessions', :action => 'destroy' dev.login '/login', :controller => 'sessions', :action => 'create' end end # config/routes/admin.rb ActionController::Routing::Routes.draw do |map| map.namespace :admin do |admin| admin.resources :posts admin.resources :comments admin.logout '/logout', :controller => 'sessions', :action => 'destroy' admin.login '/login', :controller => 'sessions', :action => 'create' end end # config/routes/api.rb ActionController::Routing::Routes.draw do |map| map.namespace :api do |api| api.resources :posts api.resources :comments api.logout '/logout', :controller => 'sessions', :action => 'destroy' api.login '/login', :controller => 'sessions', :action => 'create' end end

In Rails3, you can set the configs in config/application.rb
config.paths.config.routes.concat Dir[Rails.root.join("config/routes/*.rb")]

【下列文章您可能也有興趣】

沒有留言: