Created
August 5, 2021 13:07
-
-
Save georgiybykov/1f95c2c02c78bd35915e4c860604cc77 to your computer and use it in GitHub Desktop.
How to split routes to separate files for Rails App or Rails Engine
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# config/routes.rb | |
# frozen_string_literal: true | |
MyApplication::Application.routes.draw do | |
draw_routes_for :v1 | |
draw_routes_for :v2 | |
draw_routes_for :v3 | |
root to: 'projects#index' | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# config/initializers/routing_draw.rb | |
# frozen_string_literal: true | |
module ActionDispatch | |
module Routing | |
class Mapper | |
def draw_routes_for(routes_name) | |
instance_eval(File.read(Rails.root.join("engines/my_application/config/routes/#{api_version}.rb"))) | |
end | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# config/routes/v3.rb | |
# frozen_string_literal: true | |
Rails.application.routes.draw do | |
scope :v3, module: :v3 do | |
get '/profile/me', to: 'profiles#me' | |
post '/login', to: 'sessions#create' | |
post '/logout', to: 'sessions#destroy' | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment