我正在尝试实现一个简单的Rails API应用程序,其中包含版本控制,基于 Railscasts#350 episode。 我想通过在Accept header
中提供以下格式的版本来访问应用程序的特定API版本:application/vnd.rails5_api_test.v1
。 当未提供Accept header
时,请求将路由到应用程序的当前默认版本。 为了处理这个问题,我在我的lib directory
中创建了一个api_constraints
文件,该文件需要在路由中被引用。
v1
和 v2
,其中 v1
包含 Users resource
,而 v2
包含 Users 和 Comments resources
。
一切都按预期运行,但是当我使用 Postman 通过标题传递版本 1
请求 URL localhost:3000/comments
时,我得到的响应来自 comments resource
,并显示所有评论。但是,我希望响应为 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/comments_controller.rb:
class Api::V2::CommentsController < ApplicationController
def index
@comments = Comment.all
render json: @comments, each_serializer: ::V2::CommentSerializer
end
end
版本1的用户序列化程序,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 版本的评论序列化程序,comment_serializer.rb:
class V2::CommentSerializer < ActiveModel::Serializer
attributes :id, :description
belongs_to :user
end
我尝试删除路由中的
default: true
选项,然后它按预期工作。但是我想让它使用默认选项工作。
请问有人能告诉我哪里出错了,并分享您对此方法的想法。如果这不是最佳实现方式,请指导我正确的实现方法。感谢所有花时间帮助我的人。:)干杯!
comments_controller.rb
吗? - 7urkm3ncomments_controller.rb
。请检查并告知您的想法。感谢。 - Yash