active_model_serializer 0.10.0.rc5中的ActiveModel :: Serializer :: CollectionSerializer :: NoSerializerError [英] ActiveModel::Serializer::CollectionSerializer::NoSerializerError in active_model_serializer 0.10.0.rc5

查看:109
本文介绍了active_model_serializer 0.10.0.rc5中的ActiveModel :: Serializer :: CollectionSerializer :: NoSerializerError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将active_model_serializer 0.10.0.rc5grape gem用于api.

我有一个这样的帖子终结点:

I've a post endpoint like this :

class V1::Endpoints::Posts < Grape::API
  resource :posts do
    desc 'Returns a list of posts.'
    # serializing array
    get '', each_serializer: V1::Serializers::PostSerializer  do
      @posts = Post.all
      present @posts
    end
  end
end

我的序列化器看起来像这样:

My serializer looks something like this :

class V1::Serializers::PostSerializer < ActiveModel::Serializer
  attributes :id, :name, :slug
end

现在,当我尝试访问发布端点时,出现以下错误:

Now when I try to access the post endpoint I get the following error :

ActiveModel::Serializer::CollectionSerializer::NoSerializerError - No serializer found for resource:

我在调试问题时发现的问题在于此gem的CollectionSerializer#initialize.我想serializer_class变量将显示为零.

The issue which I figured out while debugging the issue lies in the CollectionSerializer#initialize of this gem. I suppose that the serializer_class variable is coming out to be nil.

我已经尝试了几乎所有与该问题相关的链接.但是没有一个对我有用.

I've tried almost all the links which seemed relevant for this problem. But none worked for me.

推荐答案

尝试使用序列化程序而不是each_serializer:

try to use serializer instead of each_serializer:

get '', serializer: V1::Serializers::PostSerializer  do

代替:

get '', each_serializer: V1::Serializers::PostSerializer  do

这篇关于active_model_serializer 0.10.0.rc5中的ActiveModel :: Serializer :: CollectionSerializer :: NoSerializerError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆