Split routes.rb in Rails6

Thing you want to do

I want to split the routing definition file config / routes.rb into multiple files. Specifically, I have a file called lib / my_app / routes.rb and want to load it.

Old solution

The 2015 article https://y-yagi.tumblr.com/post/118514473965/ is a hit, but it doesn't work as it is. The definition and execution of draw works well, but it fails because the mechanism for reloading seems to be old.

Recent solutions

Rails6 has a pretty organized way of loading routes so you don't have to reload it yourself. A class called Rails :: Application :: RoutesReloader is defined with the correct parameters. If you can give it, it will be completed. If you want to decompose routes.rb under config / routes /, the method described in the article below seems to be good.

https://medium.com/rubycademy/how-to-keep-your-routes-clean-in-ruby-on-rails-f7cf348ec13b

However, I couldn't read any file with this, so I chose another method.

Other solutions

The routing is loaded at ʻadd_routing_paths` in initializers. So I thought that I should set it so that path is added before the initializers.

Specifically, the following settings were added to config / application.rb.

config/application.rb


    initializer('add_my_app_routing_paths', before: 'add_routing_paths') do |app|
      routing_path = root.join('lib/my_app/routes.rb')
      app.routes_reloader.paths.prepend(routing_path)
    end

Now, as expected, the routes defined in lib / my_app / routes.rb are loaded. I didn't seem to have the side effect of not being reloaded, so I decided to go with this.

bonus

If you look at the unreleased master branch source code, it looks like below written.

    initializer :add_routing_paths do |app|
      routing_paths = paths["config/routes.rb"].existent
      external_paths = self.paths["config/routes"].paths
      routes.draw_paths.concat(external_paths)


      if routes? || routing_paths.any?
        app.routes_reloader.paths.unshift(*routing_paths)
        app.routes_reloader.route_sets << routes
        app.routes_reloader.external_routes.unshift(*external_paths)
      end
    end

When I set a variable called external_paths, I feel that if I can pass the path I want to read, I can set it better.

Recommended Posts

Split routes.rb in Rails6
Model association in Rails
Adding columns in Rails
Disable turbolinks in Rails
CSRF measures in Rails
^, $ in Rails regular expression
Use images in Rails
Understand migration in rails
Implement markdown in Rails
Implement application function in Rails
Declarative transaction in Rails #ginzarb
Japaneseize using i18n with Rails
Implement LTI authentication in Rails
Error in rails db: migrate
Display Flash messages in Rails
View monthly calendar in Rails
Implement import process in Rails
Use multiple checkboxes in Rails6!
Rewrite Routes in Rails Engine
Rails: Capture regular expressions in emails!
[Rails] Keyword search in multiple tables
[Rails] Session timeout setting in devise
Add a search function in Rails.
Enable jQuery and Bootstrap in Rails 6 (Rails 6)
[rails] Login screen implementation in devise
[Rails] How to write in Japanese
[Rails] Unexpected validation error in devise
About the symbol <%%> in Rails erb
Differences in split methods of StringUtils
[Rails] Use cookies in API mode
Implement simple login function in Rails
Create a new app in Rails
Ruby on Rails Japanese-English support i18n
[Solution] Webpacker :: Manifest :: MissingEntryError in Rails
Implement a contact form in Rails
Remove "assets" and "turbolinks" in "Rails6".
CRUD features and MVC in Rails
How to introduce jQuery in Rails 6
First pagination feature added in rails
Data is not registered in Rails.
[Rails 6] Customize Bootstrap in Rails + Bootstrap 5.0.0-alpha environment
Implement CSV download function in Rails
Ruby methods often used in Rails
How to install Swiper in Rails
How to implement search functionality in Rails
Definitions other than 7 basic actions in Rails
[Rails] Function restrictions in devise (login / logout)
How to change app name in rails
How to insert a video in Rails
The identity of params [: id] in rails
[rails] List of actions defined in Controller
Rails refactoring story learned in the field
Ruby on Rails in Visual Studio Codespaces
JavaScript (vanilla) does not respond in Rails.
How to use MySQL in Rails tutorial
Steps to set a favicon in Rails
Specify home view in rails app [root]
Implemented follow function in Rails (Ajax communication)
[Rails] About local: true described in form_with
[Rails] Store only checked items in DB
How to implement ranking functionality in Rails