命名空间下的ActiveModel序列化程序无法正常工作

时间:2014-11-30 15:14:55

标签: ruby-on-rails ruby ruby-on-rails-4 active-model-serializers

我有以下序列化器/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

这似乎没有正常工作,因为我删除了名称,它仍显示所有字段的名称。

如何更改它以使其有效?

2 个答案:

答案 0 :(得分:1)

为什么不:

render json: @clubs, each_serializer: ::Api::ClubSerializer

答案 1 :(得分:0)

您现在的操作方式:

render json: @clubs, namespace: Api

请参阅:https://github.com/rails-api/active_model_serializers/blob/0-10-stable/docs/general/rendering.md#namespace