如何修复 "Rendered ActiveModel::Serializer::Null with Hash"
how to fix "Rendered ActiveModel::Serializer::Null with Hash"
我正在尝试为用户模型编写 API,我只需要 return 两列进行一些修改(附加字符串)
一切正常,我什至得到了正确的结果,但是当我看到状态代码显示“500”时,当我检查日志时它显示以下错误
[active_model_serializers] Rendered ActiveModel::Serializer::Null with Hash
代码如下
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
- user.rb
...
def pl_url
return "#{Rails.application.secrets.app_host}/#{self.calendar_url_token}"
end
...
- 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
我正在尝试为用户模型编写 API,我只需要 return 两列进行一些修改(附加字符串)
一切正常,我什至得到了正确的结果,但是当我看到状态代码显示“500”时,当我检查日志时它显示以下错误
[active_model_serializers] Rendered ActiveModel::Serializer::Null with Hash
代码如下
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
- user.rb
...
def pl_url
return "#{Rails.application.secrets.app_host}/#{self.calendar_url_token}"
end
...
- 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