【发布时间】:2012-02-13 00:15:38
【问题描述】:
是否可以拆分 Rails 3.X routes.rb 文件?
我们有这么多资源,很难找到它们。我想至少拆分 APP 和 REST API 路由。
谢谢!
【问题讨论】:
标签: ruby-on-rails ruby
是否可以拆分 Rails 3.X routes.rb 文件?
我们有这么多资源,很难找到它们。我想至少拆分 APP 和 REST API 路由。
谢谢!
【问题讨论】:
标签: ruby-on-rails ruby
你可以这样做:
routes.rb
require 'application_routes'
require 'rest_api_routes'
lib/application_routes.rb
YourApplication::Application.routes.draw do
# Application related routes
end
lib/rest_api_routes.rb
YourApplication::Application.routes.draw do
# REST API related routes
end
更新:(此方法已从 Rails 中删除)
Rails edge 新增了一个很棒的多路径文件:
# config/routes.rb
draw :admin
# config/routes/admin.rb
namespace :admin do
resources :posts
end
这对于分解大型应用程序中的复杂路由文件非常方便。
【讨论】:
在 Rails3 中,您可以在 config/application.rb 中设置配置
config.paths.config.routes.concat Dir[Rails.root.join("config/routes/*.rb")]
【讨论】:
Dir.entries 的顺序,所以你可能需要Dir[…].sort 来依赖它。
config.paths["config/routes"]吗?您的代码引发未定义的方法错误。
Rails 3.2.11
config.paths["config/routes"].concat Dir[Rails.root.join("config/routes/*.rb")]
【讨论】: