我在我的 json api 项目中使用了 rails-api gem,为此我使用了事件模型序列化程序 gem 来序列化我的对象,但有些对象没有使用事件模型序列化程序进行序列化。
我的序列化器文件夹中有一个 MessageSerializer
class MessageSerializer < ActiveModel::Serializer
attributes :id, :sender_id, :recipient_id, :sender_type, :subj, :body, :status, :sender
def sender
object.user.try('username')
end
end
我的消息 Controller 如下
class Api::MessagesController < Api::BaseController
def index
@messages = current_user.incoming_messages
render json: @messages, serializer: MessageSerializer
end
end
但问题是抛出给客户端的序列化对象包含消息模型中的所有字段,即;它也包含 created_at、updated_at 字段。
似乎它没有使用序列化程序。
可能出了什么问题?
我搜索了很多关于它,但没有找到任何对我有帮助的帖子。
谢谢
最佳答案
在您的 BaseController 中,您是否添加了包含波纹管?include ActionController::Serialization
关于ruby-on-rails - 事件模型序列化程序不适用于 rails-api gem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26748875/