active_model_serializer 0.10.0.rc5中的ActiveModel :: Serializer :: CollectionSerializer :: NoSerializerError [英] ActiveModel::Serializer::CollectionSerializer::NoSerializerError in active_model_serializer 0.10.0.rc5
问题描述
我将active_model_serializer 0.10.0.rc5
和grape 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屋!