ruby-on-rails - 事件模型序列化程序不适用于 rails-api gem

标签 ruby-on-rails active-model-serializers rails-api

我在我的 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/

相关文章:

ruby-on-rails - 事件模型序列化程序 - 提高渲染性能

ruby-on-rails - 使用带有 RSpec 的设计测试 View

ruby-on-rails - 如何更改 Action 电缆 rails 中的 ping 间隔

javascript - PhantomJS+Capybara+RSpec 获取默认的 PhantomJs 示例域

ruby-on-rails - 如何内联调用事件模型序列化器作为哈希分配的选项

ruby-on-rails - 通过 ArraySerializer 新语法传入选项哈希

ruby-on-rails - 语法错误,意外的 keyword_ensure,期待 $end,在 HAML 中使用 form_tag

ruby-on-rails - rails 4 namespace::文件夹结构模型和 Controller

ruby-on-rails-4.1 - 事件模型序列化器不渲染我的模型

ruby-on-rails - 在 Rails 应用程序中构建 2 个中间件堆栈