我有以下序列化器/serializers/api/club_serializer.rb:
class Api::ClubSerializer < ActiveModel::Serializer
cached
attributes :id, :name, :created_at
end
以下方法/controllers/api/clubs_controller.rb
module Api
class ClubsController < BaseController
include ActionController::ImplicitRender
include ActionController::MimeResponds
# GET /clubs
def index
@clubs = Club.all
render json: @clubs, serializer: ClubSerializer
end
这似乎没有正常工作,因为我删除了名称,它仍显示所有字段的名称。
如何更改它以使其有效?
答案 0 :(得分:1)
为什么不:
render json: @clubs, each_serializer: ::Api::ClubSerializer
答案 1 :(得分:0)
您现在的操作方式:
render json: @clubs, namespace: Api