ActiveModel::Serializer::CollectionSerializer::NoSerializerError in active_model_serializer 0.10.0.rc5

陌路散爱 提交于 2021-01-02 05:55:21

问题


I'm using active_model_serializer 0.10.0.rc5 and grape gem for the 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:

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.


回答1:


try to use serializer instead of each_serializer:

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

Instead of:

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



回答2:


I ended using a DRYed version of render json: @object, serializer: Namespaced::ObjectSerializer.

Since I have found little information on this matter, I've posted this approach here: Correct way to implement API versioning with active_model_serializers

I hope it helps!



来源:https://stackoverflow.com/questions/37053962/activemodelserializercollectionserializernoserializererror-in-active-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!