2016-04-08 10 views
1

私はIonicフレームワークを使用していますが、質問はAngularJSについてです。私はjson apiをruby on railsに書いています。認証の場合はng-token-auth + devise-token-authを選択します。AngularJSはJSON APIを通じてユーザーから関連データを取得します

ユーザーJSON:ユーザーのための

{ 
    "data": { 
    "id": "1", 
    "type": "users", 
    "attributes": { 
     "name": "User1", 
     "email": "[email protected]", 
     "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": "[email protected]", 
     "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 protected]"}} 

私の問題があり、私は、ユーザーに到達することはできません関係情報。

理由はわかりませんが、私のビュー内のオブジェクト名はuser(current_userではない)であり、自分のコントローラにアクセスできません。 質問このオブジェクトからいくつかの追加情報を入手するにはどうすればいいですか、コントローラに現在のユーザーオブジェクトアクセスを提供する方法です。

答えて

0

私はそれを理解したので、答えは本当に簡単です。 ng-token-authは現在のユーザとして$rootScope.userを提供し、コントローラで使用できます。 しかし、私はまだユーザーオブジェクトに関連するデータを提供する方法を知らない、幸いにも私のニーズの回避策が見つかりました。

関連する問題