【问题标题】:Why am I getting this error in a basic Rails+Ember app?为什么我在基本的 Rails+Ember 应用程序中出现此错误?
【发布时间】:2013-09-15 10:50:18
【问题描述】:

我正在尝试使用 Ember + Rails 做一个简单的 CRUD 应用程序,但在尝试转到 /workouts 路线时出现以下错误。

Error while loading route: TypeError {} ember.js?body=1:415
Uncaught TypeError: Object function () {
    if (!wasApplied) {
      Class.proto(); // prepare prototype...
    }
    o_defineProperty(this, GUID_KEY, undefinedDescriptor);
    o_defineProperty(this, '_super', undefinedDescriptor);
    var m = meta(this), proto = m.proto;
    m.proto = this;
    if (initMixins) {
      // capture locally so we can clear the closed over variable
      var mixins = initMixins;
      initMixins = null;
      this.reopen.apply(this, mixins);
    }
    if (initProperties) {
      // capture locally so we can clear the closed over variable
      var props = initProperties;
      initProperties = null;

      var concatenatedProperties = this.concatenatedProperties;

      for (var i = 0, l = props.length; i < l; i++) {
        var properties = props[i];

        Ember.assert("Ember.Object.create no longer supports mixing in other definitions, use createWithMixins instead.", !(properties instanceof Ember.Mixin));

        for (var keyName in properties) {
          if (!properties.hasOwnProperty(keyName)) { continue; }

          var value = properties[keyName],
              IS_BINDING = Ember.IS_BINDING;

          if (IS_BINDING.test(keyName)) {
            var bindings = m.bindings;
            if (!bindings) {
              bindings = m.bindings = {};
            } else if (!m.hasOwnProperty('bindings')) {
              bindings = m.bindings = o_create(m.bindings);
            }
            bindings[keyName] = value;
          }

          var desc = m.descs[keyName];

          Ember.assert("Ember.Object.create no longer supports defining computed properties.", !(value instanceof Ember.ComputedProperty));
          Ember.assert("Ember.Object.create no longer supports defining methods that call _super.", !(typeof value === 'function' && value.toString().indexOf('._super') !== -1));
          Ember.assert("`actions` must be provided at extend time, not at create time, when Ember.ActionHandler is used (i.e. views, controllers & routes).", !((keyName === 'actions') && Ember.ActionHandler.detect(this)));

          if (concatenatedProperties && indexOf(concatenatedProperties, keyName) >= 0) {
            var baseValue = this[keyName];

            if (baseValue) {
              if ('function' === typeof baseValue.concat) {
                value = baseValue.concat(value);
              } else {
                value = Ember.makeArray(baseValue).concat(value);
              }
            } else {
              value = Ember.makeArray(value);
            }
          }

          if (desc) {
            desc.set(this, keyName, value);
          } else {
            if (typeof this.setUnknownProperty === 'function' && !(keyName in this)) {
              this.setUnknownProperty(keyName, value);
            } else if (MANDATORY_SETTER) {
              Ember.defineProperty(this, keyName, null, value); // setup mandatory setter
            } else {
              this[keyName] = value;
            }
          }
        }
      }
    }
    finishPartial(this, m);
    this.init.apply(this, arguments);
    m.proto = proto;
    finishChains(this);
    sendEvent(this, "init");
  } has no method 'find' 

我的代码在这里:https://github.com/ecl1pse/ember-workouts

我做错了什么?

编辑:经过进一步调查,我认为罪魁祸首是

EmberWorkouts.WorkoutsRoute = Ember.Route.extend(
  model: -> EmberWorkouts.Workout.find()

这实际上并没有返回任何东西。如何从那里调试?

如果我用这个替换它

EmberWorkouts.WorkoutsRoute = Ember.Route.extend
  model: -> [{title: 'hi'}, {title: 'damn'}]

视图实际呈现内容。

如何让模型正确地从 Rails 中收集?

【问题讨论】:

  • 你的错误信息是什么?,行号?
  • 这是我的错误信息。看来我的EmberWorkouts.WorkoutsRoute = Ember.Route.extend( model: -&gt; EmberWorkouts.Workout.find() 坏了。这实际上并没有做任何事情。如何从那里调试?

标签: ruby-on-rails ember.js


【解决方案1】:

Ember Data 的界面在当前版本中发生了一些变化:

  1. 您可以完全清除store.js 文件。 Ember Data 将使用 REST 适配器自动为您设置数据存储(除非您另有说明)。
  2. 请改用model: -&gt; @store.find('workout')

我用你的应用测试了这个,它可以工作。

如果您在过去一两周内没有阅读过Ember Data Guide(它发生了很大变化),我会花几分钟时间阅读。

【讨论】:

  • 我明白了:Assertion failed: You must include an id in a hash passed to push - 不过谢谢。好提示。我会努力解决的……
  • 我还是想不通。更新了回购以反映我的障碍。任何想法? @dylan
  • 解决办法是什么?说您可能找到了解决方案但不发布它很可能是您在此网站上可以做的最烦人的事情。
  • 不确定是否有其他人遇到You must include an 'id' in a hash passed to push 问题,但对我来说,解决方法是通过序列化程序的 JSON 响应传递一个 id。此外,ember 模型不能指定 id 属性。
【解决方案2】:

对我来说,这个错误的修复(从 ember-data 1.0.0.beta.6 开始)是确保从服务器返回的 JSON 包含每个模型的“id”字段,但不明确声明设置 Ember DS.Model 时的 id。

jbuilder 模板:

json.scans do
  json.array! @scans do |scan|
    json.id scan.id # This prop has to be there
    json.name scan.name
  end
end

Ember 模型:

EmberApp.Scan = DS.Model.extend(
  // Don't include the id prop here
  name: DS.attr("string")
)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-11
    • 1970-01-01
    • 2019-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多