How to split routes.rb into multiple files [duplic

2019-06-12 16:36发布

问题:

This question already has an answer here:

  • Splitting Routes File Into Multiple Files 4 answers

I have huge routes.rb file and I want to split into multiple manageable files.

As suggested in following article, I have created separated folder for routes and I have created multiple routes files in this folder Link: http://rails-bestpractices.com/posts/73-split-route-namespaces-into-different-files

routes.rb
routes/user.rb
routes/manager.rb
routes/admin.rb
routes/anonymous.rb

and in my application.rb, I set the config.paths value. I used various possible combination but I am still unable to load all secondary routes files.

Here are the list of code I used to set config.paths in application.rb file. None are working for me.

config.paths["config/routes"].concat(Dir[Rails.root.join("config/routes/*.rb")])
config.paths["config/routes"] = Dir[Rails.root.join("config/routes/*.rb")]
config.paths["config/routes"] = Dir[Rails.root.join("config/routes/*.rb")].each{|r| config.paths["config/routes"].unshift(r) }
config.paths.config.routes.concat Dir[Rails.root.join("config/routes/*.rb")]

Appreciate if someone can help me. Please note that I am using Rails 3.2.1. I am sure that above techniques of splitting routes will work with previous version of Rails but I am unable to implement using 3.2.1.

回答1:

All right. I am able to load all secondary routes in main routes.rb. Looks dirty but it's working in Rails 3.2.1.

Acme::Application.routes.draw do
  resources :users

  Dir[Rails.root.join("config/routes/*.rb")].each{|r| load(r)}

  resources :messages
  match '*path' => 'cms/pages#show'
  root :to => "home#index", :port => false
end

Any cleaner approach is much welcome.