【发布时间】:2016-04-01 15:45:21
【问题描述】:
我正在尝试基于Railscasts #350 episode 实现一个简单的带有版本控制的rails api 应用程序。我想通过 Accept header 以这种格式:application/vnd.rails5_api_test.v1 提供版本来访问应用程序的特定 API 版本。当没有提供Accept header 时,请求将被路由到应用程序的当前默认版本。为了解决这个问题,我在我的lib directory 中创建了一个api_constraints 文件,该文件是路由所必需的。
我创建了两个版本的应用程序v1 和v2,其中v1 有Users resource,v2 有Users and Comments resources。
一切都按预期工作,除了当我通过使用 Postman 通过标头传递版本 1 来请求 URL localhost:3000/comments 时,我得到了来自 comments resource 的响应,显示了所有 cmets。但我希望响应为status: 404 Not Found,因为comments resource 的版本为2,而请求的版本为1。
这是来自服务器的响应:
Started GET "/comments" for 127.0.0.1 at 2016-04-01 20:57:53 +0530
Processing by Api::V2::CommentsController#index as application/vnd.rails5_api_test.v1
Comment Load (0.6ms) SELECT "comments".* FROM "comments"
[active_model_serializers] User Load (0.9ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
[active_model_serializers] Rendered ActiveModel::Serializer::CollectionSerializer with ActiveModelSerializers::Adapter::JsonApi (4.32ms)
Completed 200 OK in 7ms (Views: 5.0ms | ActiveRecord: 1.5ms)
这是我的工作文件:
约束文件,lib/api_constraints.rb:
class APIConstraints
def initialize(options)
@version = options[:version]
@default = options[:default]
end
def matches?(req)
req.headers["Accept"].include?(media_type) || @default
end
private
def media_type
"application/vnd.rails5_api_test.v#{@version}"
end
end
路由文件,config/routes.rb:
Rails.application.routes.draw do
require "api_constraints"
scope module: 'api/v1', constraints: APIConstraints.new(version: 1) do
resources :users
end
scope module: 'api/v2', constraints: APIConstraints.new(version: 2, default: true) do
resources :users
resources :comments
end
end
v1 的用户控制器,api/v1/users_controller.rb:
class Api::V1::UsersController < ApplicationController
def index
@users = User.all
render json: @users, each_serializer: ::V1::UserSerializer
end
end
v2 的用户控制器,api/v2/users_controller.rb:
class Api::V2::UsersController < Api::V1::UsersController
def index
@users = User.all
render json: @users, each_serializer: ::V2::UserSerializer
end
end
v2 的评论控制器,api/v2/cmets_controller.rb:
class Api::V2::CommentsController < ApplicationController
def index
@comments = Comment.all
render json: @comments, each_serializer: ::V2::CommentSerializer
end
end
v1 的用户序列化程序,user_serializer.rb:
class V1::UserSerializer < ActiveModel::Serializer
attributes :id, :name, :email
end
v2 的用户序列化器,user_serializer.rb:
class V2::UserSerializer < V1::UserSerializer
has_many :comments
end
用于 v2 的 cmets 序列化程序,comment_serializer.rb:
class V2::CommentSerializer < ActiveModel::Serializer
attributes :id, :description
belongs_to :user
end
我尝试删除路由中的default: true 选项,然后它按预期工作。但我希望它使用默认选项。
谁能告诉我我在哪里弄错了,并分享你对这种方法的想法。如果这不是最好的方法,请指导我完成正确的实施方法。提前感谢任何花时间帮助我的人。 :) 干杯!
【问题讨论】:
-
我也可以看看你的
comments_controller.rb -
哎呀!我错过了那个文件。用
comments_controller.rb更新了帖子。检查并让我知道您是否有任何想法。谢谢。
标签: ruby-on-rails ruby api routes versioning