将routes.rb 文件分成多个文件以便更好地管理的好方法是什么?

发布于 2024-10-01 00:37:10 字数 538 浏览 0 评论 0原文

我正在开发一个 Rails 3 项目,该项目有一个相当大的路线文件。它利用了一些嵌套,我遇到了一个问题,主要是由于路由文件难以管理。

有没有办法把它分成多个文件?

比如:

My::Application.routes.draw do
  constraints(:subdomain => 'admin') do
    include My::Application::Routes::AdminRoutes
  end

  include My::Application::Routes::MainRoutes
end

或者……

My::Application.routes.draw do
  constraints(:subdomain => 'admin') do
    require 'routes/admin_routes.rb'
  end

  require 'routes/main_routes.rb'
end

或者类似的东西。

谢谢!

I am working on a rails 3 project with a fairly large routes file. It takes advantage of some nesting and I ran into an issue due largely to the fact that the routes files is difficult to manage.

Is there a way to break it up into multiple files?

Something like:

My::Application.routes.draw do
  constraints(:subdomain => 'admin') do
    include My::Application::Routes::AdminRoutes
  end

  include My::Application::Routes::MainRoutes
end

Or...

My::Application.routes.draw do
  constraints(:subdomain => 'admin') do
    require 'routes/admin_routes.rb'
  end

  require 'routes/main_routes.rb'
end

Or something along those lines.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

香橙ぽ 2024-10-08 00:37:10

include 将包含的模块的方法插入到命名空间中,而 require 只是将文件加载到顶级命名空间中。这些都不适合你。

只需加载单独的文件

My::Application.routes.draw do
  constraints(:subdomain => 'admin') do
    load 'routes/admin_routes.rb'
  end

  load 'routes/main_routes.rb'
end

include inserts the included module's methods into the namespace, and require just loads the file into the top level namespace. None of those will work for you.

Just load the seperate files

My::Application.routes.draw do
  constraints(:subdomain => 'admin') do
    load 'routes/admin_routes.rb'
  end

  load 'routes/main_routes.rb'
end
全部不再 2024-10-08 00:37:10

您可以使用的另一个选项

ActionController::Routing::Routes.draw do |map| #routes.rb

  extend NewConnections

  some_method(map)  

end 


module NewConnections #/lib/new_connections.rb

  def some_method(clazz)
    clazz.root :controller => "demo"
  end

end

是将应用程序的根连接到默认控制器

another option you may use

ActionController::Routing::Routes.draw do |map| #routes.rb

  extend NewConnections

  some_method(map)  

end 


module NewConnections #/lib/new_connections.rb

  def some_method(clazz)
    clazz.root :controller => "demo"
  end

end

this'll connect root of your application to default controller

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文