Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/53.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails ActiveModel::Serializer为空的has_one关系(而不是JSON)返回null 问题_Ruby On Rails_Json_Serialization - Fatal编程技术网

Ruby on rails ActiveModel::Serializer为空的has_one关系(而不是JSON)返回null 问题

Ruby on rails ActiveModel::Serializer为空的has_one关系(而不是JSON)返回null 问题,ruby-on-rails,json,serialization,Ruby On Rails,Json,Serialization,我们的API将返回JSON对象(但不是JSON API)。 如何更改ActiveModel::Serializer以创建数据属性设置为null的JSON对象 我们得到的空(has_one relationship)资源的响应为NULL,但我们希望它是JSON格式{data:NULL},就像我们收到的非空资源{data:{…}或列表(has_许多关系)资源{data:[]}的响应一样 我们所做的 我们使用ActiveModel::Serialiser并指定要命名为“data”的键,而不是资源名(类

我们的API将返回JSON对象(但不是JSON API)。 如何更改ActiveModel::Serializer以创建数据属性设置为null的JSON对象

我们得到的空(has_one relationship)资源的响应为NULL,但我们希望它是JSON格式{data:NULL},就像我们收到的非空资源{data:{…}或列表(has_许多关系)资源{data:[]}的响应一样

我们所做的 我们使用ActiveModel::Serialiser并指定要命名为“data”的键,而不是资源名(类似于JSON API,但数据的内容是实体的直接JSON表示)

型号

class User < ApplicationRecord
  has_one :profile

  def serializer_class
    V1::UserSerializer
  end
end

class Profile < ApplicationRecord
  belongs_to :user

  def serializer_class
    V1::ProfileSerializer
  end
end
class ApplicationSerializer < ActiveModel::Serializer
  def json_key
    'data'
  end
end

class UserSerializer < ApplicationSerializer
  attributes :id, :created_at, :updated_at #we do not include the profile here
end

class ProfileSerializer < ApplicationSerializer
  attributes :id, :created_at, :updated_at
end
class ProfilesController < ::ApplicationController
  before_action :authenticate_user!
  def show
    @profile = current_user.profile
    render json: @profile
  end
end
我们正确地得到了非空资源的响应,它如下所示(不是JSON API):

我们想要什么 如果没有相关实体(尚未),我们希望响应的格式如下:

我发现这个解决方案解决了这个问题:

  def show
    @profile = current_user.profile
    if @profile
      render json: @profile
    else
      render json: { data: nil }
    end
  end
我发现这个解决方案解决了这个问题:

  def show
    @profile = current_user.profile
    if @profile
      render json: @profile
    else
      render json: { data: nil }
    end
  end

我们决定采用这个更严格的解决方案,在这个方案中,我们用404来响应一个空资源

def show
  @profile = current_user.profile
  raise ActiveRecord::RecordNotFound unless @profile
  render json: @profile
end
在ApplicationController中,我们添加以下内容来处理异常:

rescue_from ActiveRecord::RecordNotFound do |exception|
  render json: Api::ErrorSerializer.serialize(:not_found, 'Not Found'),
         status: :not_found
end

我们决定采用这个更严格的解决方案,在这个方案中,我们用404来响应一个空资源

def show
  @profile = current_user.profile
  raise ActiveRecord::RecordNotFound unless @profile
  render json: @profile
end
在ApplicationController中,我们添加以下内容来处理异常:

rescue_from ActiveRecord::RecordNotFound do |exception|
  render json: Api::ErrorSerializer.serialize(:not_found, 'Not Found'),
         status: :not_found
end