What is a good way to split a routes.rb file into multiple files for better management?

I am working on a rails 3 project with a rather large route file. It takes advantage of nesting, and I ran into a problem, mainly because route files are difficult to manage.

Is there a way to split it into multiple files?

Sort of:

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 like that.

Thank!

+3
source share
2 answers

includeinserts the included methods of the module into the namespace, and requiresimply loads the file into the top-level namespace. None of these will work for you.

Just loadseparate files

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

  load 'routes/main_routes.rb'
end
+6

,

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

0

Source: https://habr.com/ru/post/1773634/


All Articles