【问题标题】:JsonApi Serializer Not Found [duplicate]未找到 JsonApi 序列化程序 [重复]
【发布时间】:2021-06-19 22:52:42
【问题描述】:

我已经运行了gem 'jsonapi-serializer'bundle install

然后我尝试运行生成器rails g serializer User username id,但命令总是冻结。所以我手动创建了序列化器:

# app/serializers/user_serializer.rb
class UserSerializer
  include JSONAPI::Serializer
  attributes :username, :id
end

但是当我从UsersController 调用它时,我得到一个错误:

# app/controllers/user_controller.rb
class UsersController < ApplicationController
  def show
    render json: { user: UserSerializer.new(User.new(username: 'joe'))}
  end
end

这里是错误:

NameError in UsersController#show
uninitialized constant UsersController::UserSerializer

UserSerializer 似乎没有加载,我不确定在哪里可以修改以使其工作。

【问题讨论】:

    标签: ruby-on-rails ruby active-model-serializers jsonapi-serialize


    【解决方案1】:

    我认为这是 spring loader 的问题。

    我发现这个解决方案对我有用: https://stackoverflow.com/a/60688929/3755529

    【讨论】:

      猜你喜欢
      • 2020-07-03
      • 2015-09-18
      • 2021-07-30
      • 1970-01-01
      • 1970-01-01
      • 2019-12-14
      • 2021-01-05
      • 1970-01-01
      • 2012-01-12
      相关资源
      最近更新 更多