【问题标题】:AngularJS get related data from user through JSON APIAngularJS 通过 JSON API 从用户那里获取相关数据
【发布时间】:2016-04-08 15:08:37
【问题描述】:

我正在使用 Ionic 框架,但问题是关于 AngularJS。我已经在 ruby​​ on rails 上编写了 json api。对于身份验证,我选择ng-token-auth + devise-token-auth

用户json:

{
  "data": {
    "id": "1",
    "type": "users",
    "attributes": {
      "name": "User1",
      "email": "email@email.com",
      "current-sign-in-at": "..",
      "last-sign-in-at": "..",
      "created-at": ".."
    },
    "relationships": {
      "friends": {
        "data": [
              {
                  "id": "2",
                  "type": "users"
              }
         ]
       }
    }
  },
  "included": [
    {
      "id": "2",
      "type": "users",
      "attributes": {
        "name": "User2",
        "email": "user@user.com",
        "current-sign-in-at": "..",
        "last-sign-in-at": "..",
        "created-at": ".."
      },
      "relationships": {
        "friends": {
             "data": [ ]
         }
      }
    }
  ]
}

我的用户序列化器:

class UserSerializer < ActiveModel::Serializer
  attributes :id, :name, :email,:current_sign_in_at,
              :last_sign_in_at, :created_at
  has_many :friends, through: :friendships
end

当前用户对象作为响应:

{"success":true,"data":{"id":1,"provider":"email","uid":"0a56bb6b-dc72-4ef3-906e-1c17cb2fef46","name"
:"User1","nickname":null,"image":null,"email":"email@email.com"}}

这是我的问题,我无法获取用户关系信息。

我不知道为什么,但我认为对象名称是用户(不是 current_user),并且在我的控制器中无权访问它。 问题是我如何从这个对象中获得一些额外的信息,以及如何为我的控制器提供当前用户对象访问权限。

【问题讨论】:

    标签: ruby-on-rails angularjs json api ionic-framework


    【解决方案1】:

    我想通了,所以答案很简单。 ng-token-auth 提供$rootScope.user 作为当前用户,它可以在控制器中使用。 但我仍然不知道如何向用户对象提供相关数据,幸运的是找到了满足我需求的解决方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-28
      • 2015-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多