How to split routes.rb into multiple files [duplicate]

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-03 05:10:12

问题


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.



来源:https://stackoverflow.com/questions/10809811/how-to-split-routes-rb-into-multiple-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!