Answer the question
In order to leave comments, you need to log in
How to specify namepsace in rails routing?
Hello everyone
In rails routing, to ensure api versioning, there is such a construction:
Rails.application.routes.draw do
concern :versioned_api_methods do
mount_devise_token_auth_for 'User', at: 'auth', controllers: {
sessions: 'overrides/sessions',
}
resources :users, only: %i[show update]
end
namespace :v1 do
concerns :versioned_api_methods
end
namespace :v2 do
concerns :versioned_api_methods
end
end
Answer the question
In order to leave comments, you need to log in
Use Constraints
# app/constraints/version_constraint.rb
class VersionConstraint
def initialize
@versions = %w[ v1 v2 ]
end
def matches?(request)
@versions.include? request.path_parameters[:version].to_s
end
end
#routes.rb
scope ':version', constraints: VersionConstraint.new do
concerns :versioned_api_methods
end
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question