【发布时间】:2016-02-13 16:40:28
【问题描述】:
我正在开发一个使用 rails 5 api(测试版)编写后端的应用程序。
我的 API 会有一些版本,我正在使用这种方法来解决版本控制问题:
https://github.com/iamvery/rails-api-example/blob/master/config/routes.rb
Rails.application.routes.draw do
def api_version(version, &routes)
api_constraint = ApiConstraint.new(version: version)
scope(module: "v#{version}", constraints: api_constraint, &routes)
end
api_version(1) do
resources :articles, only: :index
end
api_version(2) do
resources :articles, only: :index
end
end
问题是当我没有指定版本时,它会显示一个(明显的)错误(ActionController::RoutingError: No route matches [GET] \...)。
但我想使用最新的 api 版本进行路由,而不是抛出错误。
【问题讨论】:
-
不指定版本时,您希望 url 看起来如何?
-
我通过标头传递 api 版本号。当我没有指定特定版本时,rails 应该使用默认版本。
标签: ruby-on-rails rails-api ruby-on-rails-5