如何修复"Rendered ActiveModel::Serializer::Null with Hash"



>我正在尝试为用户模型编写API,其中我只需要返回两列并进行一些修改(附加字符串(
一切都很好,我什至得到了正确的结果,但是当我看到状态代码显示"500"时,当我检查日志时,它显示以下错误

[active_model_serializers] 呈现的活动模型::序列化程序::带哈希的空

以下是代码
1. users_controller.rb

class Api::V1::UsersController < Api::V1::ApiController
  # GET
  def pl_details
    render json: {pl: current_user.pl_url, enabled: current_user.personal_calendar_enabled}, status: :success
  end
...
end
  1. 用户.rb
...
def pl_url
  return "#{Rails.application.secrets.app_host}/#{self.calendar_url_token}"
end
...
  1. user_serializer.rb
class UserSerializer < ActiveModel::Serializer
  attributes :id, :firstname, :lastname, :email
end

没关系,
我只是以另一种方式做了,我使用了一个单独的序列化程序来避免错误,以下是方法

class Api::V1::UsersController < Api::V1::ApiController
  # GET
  def pl_details
    render json: current_user,serializer: PLSerializer, status: :success
  end
...
end

和内部 PLSerializer

class PLSerializer < ActiveModel::Serializer
  attributes :pl, :personal_calendar_enabled
  def personal_link
    current_user.pl_url
  end
end

最新更新