Skip to content

Instantly share code, notes, and snippets.

@georgiybykov
Created August 5, 2021 13:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save georgiybykov/1f95c2c02c78bd35915e4c860604cc77 to your computer and use it in GitHub Desktop.
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
# 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
# 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
# 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