Divide large routes.rb to multiple files in Rails 5
Asked Answered
S

4

19

I want to upgrade my rails 4 app to 5.0.0.beta2. Currently I divided the routes.rb file to multiple files by setting config.paths["config/routes.rb"] e.g.,

module MyApp
  class Application < Rails::Application
    config.paths["config/routes.rb"]
      .concat(Dir[Rails.root.join("config/routes/*.rb")])
  end
end

It seems rails 5.0.0.beta2 also exposes config.paths["config/routes.rb"] but the above code doesn't work. How can I divide routes.rb file in rails 5?

Sacrarium answered 24/2, 2016 at 17:31 Comment(1)
Check out this answer, works on any Rails afaikBonucci
M
23

Rails 6.1+ built-in way to load routes from multiple files.

From official Rails docs:


Breaking up very large route file into multiple small ones:

If you work in a large application with thousands of routes, a single config/routes.rb file can become cumbersome and hard to read.

Rails offers a way to break a gigantic single routes.rb file into multiple small ones using the draw macro.

# config/routes.rb

Rails.application.routes.draw do
  get 'foo', to: 'foo#bar'

  draw(:admin) # Will load another route file located in `config/routes/admin.rb`
end

# config/routes/admin.rb

namespace :admin do
  resources :comments
end

Calling draw(:admin) inside the Rails.application.routes.draw block itself will try to load a route file that has the same name as the argument given (admin.rb in this case). The file needs to be located inside the config/routes directory or any sub-directory (i.e. config/routes/admin.rb or config/routes/external/admin.rb).

You can use the normal routing DSL inside the admin.rb routing file, however you shouldn't surround it with the Rails.application.routes.draw block like you did in the main config/routes.rb file.


Link to the corresponding PR.

Merissameristem answered 20/9, 2020 at 19:13 Comment(1)
Cool, I'll migrate to the official way after 6.1. Thanks!Sacrarium
U
10

you can write some codes in config/application.rb

config.paths['config/routes.rb'] = Dir[Rails.root.join('config/routes/*.rb')]
Undervest answered 10/3, 2016 at 8:11 Comment(2)
Thanks a lot. This solution also auto reloads the routes on code change. There is more information here: makandracards.com/makandra/… Pay attention that if in your original routes your code was under scope your new routs file also need to have that scopeStultify
This solution doesn't seems to work when you have concerns defined and reference them from one route file to another.Pole
I
10

Here's a nice article, simple, concise, straight to the point - not mine.

config/application.rb

module YourProject
  class Application < Rails::Application
    config.autoload_paths += %W(#{config.root}/config/routes)
  end
end

config/routes/admin_routes.rb

module AdminRoutes
  def self.extended(router)
    router.instance_exec do
      namespace :admin do
        resources :articles
        root to: "dashboard#index"
      end
    end
  end
end

config/routes.rb

  Rails.application.routes.draw do
    extend AdminRoutes

    # A lot of routes
  end
Icily answered 10/8, 2017 at 6:10 Comment(3)
Is it possible to use this solution and it the rails will auto reload when changing the routes file?Stultify
Rails seems offer an official way for such a case which looks easier. edgeguides.rubyonrails.org/…Mull
@MitsutoshiWatanabe The draw approach is only available in Rails 6.1 and laterColorado
R
5

I like the method demonstrated in this gist and expanded on in this blog post:

class ActionDispatch::Routing::Mapper
  def draw(routes_name)
    instance_eval(File.read(Rails.root.join("config/routes/#{routes_name}.rb")))
  end
end

BCX::Application.routes.draw do
  draw :api
  draw :account
  draw :session
  draw :people_and_groups
  draw :projects
  draw :calendars
  draw :legacy_slugs
  draw :ensembles_and_buckets
  draw :globals
  draw :monitoring
  draw :mail_attachments
  draw :message_preview
  draw :misc

  root to: 'projects#index'
end
Renarenado answered 30/10, 2019 at 17:48 Comment(1)
We've been using this for quite some time but unfortunately it does not auto-reload the routes and you'll have to start/stop the serverPrincessprinceton

© 2022 - 2024 — McMap. All rights reserved.