Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/63.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 在Emberjs中获取sigleton嵌套的单例资源_Ruby On Rails_Api_Ember.js_Ember Data - Fatal编程技术网

Ruby on rails 在Emberjs中获取sigleton嵌套的单例资源

Ruby on rails 在Emberjs中获取sigleton嵌套的单例资源,ruby-on-rails,api,ember.js,ember-data,Ruby On Rails,Api,Ember.js,Ember Data,我正在尝试获取具有以下路径的单例资源:/api/users/:user\u id/profile。用户模型对概要文件一无所知。这些模型在Rails中定义如下: class UserProfile < ActiveRecord::Base belongs_to :user end class-UserProfile

我正在尝试获取具有以下路径的单例资源:
/api/users/:user\u id/profile
。用户模型对概要文件一无所知。这些模型在Rails中定义如下:

class UserProfile < ActiveRecord::Base
  belongs_to :user
end
class-UserProfile

如何定义EmberJS模型和路由,以便在转到
/users/1/profile
时获取用户的配置文件?我一直在试图找到如何在EmberJS中定义单例资源,但找不到任何合适的方法。

我认为您需要一个适配器,比如:

适配器/user profile.js

import ActiveModelAdapter from 'active-model-adapter';

export default ActiveModelAdapter.extend({
  namespace: 'api/users',
  pathForType() {
    return 'user_profile';
  },

  urlForFindRecord(id, modelName) {
    return this.urlForFindAll(modelName);
  },

  urlForUpdateRecord(id, modelName) {
    return this.urlForFindAll(modelName);
  }
});
“用户对配置文件一无所知”是否意味着
/API/users/:user\u id
的API响应不包括对
/API/users/:user\u id/profile
的任何引用?如果是这样的话,你可以告诉余烬它是这样做的

假设JSON-API:

// serializer:user
normalize(type, hash, prop) {
  const result = this._super(type, hash ,prop);
  const userId = result.data.id;
  result.relationships.userProfile = {
    links: { related: `/api/users/${userId}/profile` }
  };
  return result;
}
或者,如果您使用的是旧的余烬数据:

// serializer:user
normalizeRelationships(type, hash) {
  hash.links = hash.links || {};
  hash.links.userProfile = `/api/users/${userId}/profile`;
  return this._super(type, hash);
}
然后,
model:user
可以具有
userProfile
关系:

// model:user
userProfile: DS.belongsTo('userProfile', { async: true })

听起来你在找我,谢谢你的回复!这是有道理的。我有一些担心,因为ember js不太擅长处理单例资源。