获取断言失败:当我导航到Ember使用Rails API

时间:2018-06-14 18:37:45

标签: ruby-on-rails ember.js ember-data active-model-serializers rails-api

我有一个使用ember-data来使用我的Rails RESTful API的Ember应用程序。每次我导航或刷新Leases路线时,我都会获得3,有时还会有6 Error: Assertion Failed: You can no longer pass a modelClass as the first argument to store._buildInternalModel. Pass modelName instead.

Ember-data能够从rails api成功检索有效负载。在挖掘之后,看起来问题可能是由串行器中的EmbeddedRecordsMixin引起的。但是,我没有覆盖Lease的序列化程序。

这是我对rails后端的设置:

class LeaseSerializer < ActiveModel::Serializer
  attributes :id, :start_date, :end_date, :rent, :description, :unit_id, 
  :tenant_id, :landlord_id
end

class Lease < ApplicationRecord
   belongs_to :unit, inverse_of: :leases
   belongs_to :landlord, inverse_of: :leases
   belongs_to :tenant, inverse_of: :leases
end

class LandlordSerializer < ActiveModel::Serializer
   attributes :id, :first_name, :last_name

   has_many :leases
   has_many :places
end

class Landlord < ApplicationRecord
  belongs_to :user
  has_many :places, inverse_of: :landlord
  has_many :units, through: :places
  has_many :leases, inverse_of: :landlord
  has_many :tenants, through: :leases
end

class Unit < ApplicationRecord
  belongs_to :place
  has_many :leases
end

class UnitSerializer < ActiveModel::Serializer
  attributes :id, :unit_num, :beds, :baths, :place_id
  has_many :leases
end

class Tenant < ApplicationRecord
  belongs_to :user
  has_many :leases
  has_many :tickets, through: :leases
end

class TenantSerializer < ActiveModel::Serializer
  attributes :id, :first_name, :last_name
  has_many :leases
end

这是我对Ember的设置:

//LEASE MODEL
export default DS.Model.extend({
  startDate:    DS.attr('date'),
  endDate:      DS.attr('date'),
  rent:         DS.attr('string'),
  description:  DS.attr('string'),

  tenant:       DS.belongsTo('tenant', { async: true }),
  unit:         DS.belongsTo('unit', { async: true }),
});

//LEASES ROUTE
export default Ember.Route.extend({
  model(){
    return this.store.findAll('lease');
  },
});

//LANDLORD MODEL
export default DS.Model.extend({
  firstName:    DS.attr('string'),
  lastName:     DS.attr('string'),

  leases:       DS.hasMany('lease', { async: true }),
  places:       DS.hasMany('place', { async: true }),
  units:        DS.hasMany('unit', { async: true })
});

//LANDLORD SERIALIZER
import { ActiveModelSerializer } from 'active-model-adapter';
import DS from 'ember-data';

export default ActiveModelSerializer.extend(DS.EmbeddedRecordsMixin, {
  attrs: {
    leases: { embedded: 'always'},
    places: { embedded: 'always'}
  }
});

//TENANT MODEL
import DS from 'ember-data';

export default DS.Model.extend({
  firstName:  DS.attr(),
  lastName:   DS.attr(),
  leases:     DS.hasMany('lease', { async: true }),
});

//TENANT SERIALIZER
import { ActiveModelSerializer } from 'active-model-adapter';
import DS from 'ember-data';

export default ActiveModelSerializer.extend(DS.EmbeddedRecordsMixin, {
  attrs: {
    leases: { embedded: 'always'},
  }
});

//UNIT MODEL

export default DS.Model.extend({
  unitNum:  DS.attr('string'),
  beds:     DS.attr('string'),
  baths:    DS.attr('string'),
  leases:   DS.hasMany('lease', { async: true })
});

在我的租赁路线模板中,我有:

{{outlet}}
<h3>Leases</h3>
{{!-- <button type="button" name="button" {{action "createLease"}}>NEW LEASE</button> --}}

<ul>
  {{#each model as |m|}}
    <li>Date:   {{m.startDate}} - {{m.endDate}}</li>
    <li>Rent:   {{m.rent}}</li>
    <li>{{m.description}}</li>
    <li>Unit:{{m.unit.unitNum}}</li>
    <li>{{#link-to "tenant" m.tenant}}
      {{m.tenant.firstName}}
    {{/link-to}}</li>
    _________________________
  {{/each}}
</ul>

请告诉我是否应该发布相关的其他内容。任何帮助都会非常感激!

0 个答案:

没有答案