Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/56.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 如何在JSON API输出中包含模型_Ruby On Rails_Ruby_Json_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails 如何在JSON API输出中包含模型

Ruby on rails 如何在JSON API输出中包含模型,ruby-on-rails,ruby,json,ruby-on-rails-4,Ruby On Rails,Ruby,Json,Ruby On Rails 4,您好,我正在创建一个API。我有一个用户和个人资料模型。用户有一个配置文件 当我得到一个用户端点时,我希望在端点中包含配置文件信息(属性) 到目前为止,我的终点是这样的 { "data" => { "id" => user.id.to_s, "type" => "users", "attributes" => { "email" => user.email }, "relation

您好,我正在创建一个API。我有一个
用户
个人资料
模型。用户有一个配置文件

当我得到一个用户端点时,我希望在端点中包含配置文件信息(属性)

到目前为止,我的终点是这样的

 {
    "data" => {
      "id" => user.id.to_s,
      "type" => "users",
      "attributes" => {
        "email" => user.email
      },
      "relationships" => {
        "profile" => {
          "data" => {
            "id" => user.profile.id.to_s,
            "type" => "profiles"
          }
        }
      }
    }
  }
  {
    "data" => {
      "id" => User.last.id.to_s,
      "type" => "users",
      "attributes" => {
        "email" => new_email
      },
      "relationships" => {
        "profile" => {
          "data" => {
            "id" => User.last.profile.id.to_s,
            "type" => "profiles"
          }
        }
      }
    }],
    "included": [
      {
        "type": "profiles",
        "id": "42",
        "attributes": {
          "first_name": "John",
          "last_name": "Smith"
        }
      }
    ]
  }
应该是这样的

 {
    "data" => {
      "id" => user.id.to_s,
      "type" => "users",
      "attributes" => {
        "email" => user.email
      },
      "relationships" => {
        "profile" => {
          "data" => {
            "id" => user.profile.id.to_s,
            "type" => "profiles"
          }
        }
      }
    }
  }
  {
    "data" => {
      "id" => User.last.id.to_s,
      "type" => "users",
      "attributes" => {
        "email" => new_email
      },
      "relationships" => {
        "profile" => {
          "data" => {
            "id" => User.last.profile.id.to_s,
            "type" => "profiles"
          }
        }
      }
    }],
    "included": [
      {
        "type": "profiles",
        "id": "42",
        "attributes": {
          "first_name": "John",
          "last_name": "Smith"
        }
      }
    ]
  }
这是我的用户序列化程序

class UserSerializer < ActiveModel::Serializer
  attributes :email
  has_one :profile
end
class UserSerializer
这是我在用户控制器中的显示操作

module V1
  class UsersController < ApiController
    before_action :set_user, only: [:show, :update, :destroy]

    def show
      render json: @user, include: params[:include], status: :ok
    end


    private

    def set_user
      @user = User.find(params[:id])
    end

    def user_params
      attribute_params.permit(:email, :password, roles: [])
    end

    def assign_roles
    end
  end
end
模块V1
类UsersController
它的设置方式非常完美。我所要做的就是在uri中传递它

像这样

 {
    "data" => {
      "id" => user.id.to_s,
      "type" => "users",
      "attributes" => {
        "email" => user.email
      },
      "relationships" => {
        "profile" => {
          "data" => {
            "id" => user.profile.id.to_s,
            "type" => "profiles"
          }
        }
      }
    }
  }
  {
    "data" => {
      "id" => User.last.id.to_s,
      "type" => "users",
      "attributes" => {
        "email" => new_email
      },
      "relationships" => {
        "profile" => {
          "data" => {
            "id" => User.last.profile.id.to_s,
            "type" => "profiles"
          }
        }
      }
    }],
    "included": [
      {
        "type": "profiles",
        "id": "42",
        "attributes": {
          "first_name": "John",
          "last_name": "Smith"
        }
      }
    ]
  }
get”/v1/用户?页面[number]=1和页面[size]=3&include=profile

在我的表演中
params[:include]
=“profile”

如果您使用的是AMS 0.9.3,您可以使用#序列化#选项传递选项,请参阅