如何将route.rb拆分为多个文件 [英] How to split routes.rb into multiple files
问题描述
我的路由文件很大,我想分割成多个可管理的文件。
I have huge routes.rb file and I want to split into multiple manageable files.
如以下文章所述,我为路由创建了单独的文件夹,已在此文件夹
中创建了多个路由文件: http://rails-bestpractices.com/posts/73-split-route-namespaces-into-different-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
,并在我的application.rb中设置config.paths值。我使用了各种可能的组合,但仍然无法加载所有辅助路由文件。
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.
这是我用来在application.rb文件中设置config.paths的代码列表。没有人为我工作。
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")]
感谢有人可以帮助我。请注意,我正在使用Rails 3.2.1。我确信上述拆分路线的技术将在Rails的早期版本中可用,但是我无法使用3.2.1来实现。
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.
推荐答案
好的。我可以将所有辅助路由加载到main route.rb中。看起来很脏,但是可以在Rails 3.2.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.
这篇关于如何将route.rb拆分为多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!