I have a very simple toy app using Rails as an API and Ember 1.8.1 with CLI for the client.
I have a typical sidebar setup where when you click the item, it displays next to the list. Everything works fine until you refresh the page with a list item on the page (a url that hits the package route), I get the following error:
Uncaught Error: Assertion Failed: The value that #each loops over must be an Array. You passed <sm-client@model:package::ember387:63>
packages/index.hbs
<div class='sidebar'> <ul>
{{#each package in model}}
<li>
{{#link-to 'package' package}}
{{package.name}}
{{/link-to}}
</li>
{{else}}
<li>No contacts found.</li>
{{/each}}
</ul>
</div>
application.hbs
<h2 id="title">App</h2>
{{link-to 'Packages' 'packages'}}
{{link-to 'Home' 'index'}}
{{outlet 'sidebar'}}
{{outlet}}
package.hbs
<dl>
<dt><strong>Name</strong></dt>
<dd>{{model.name}}</dd>
<dt><strong>Description</strong></dt>
<dd>{{model.description}}</dd>
<ul><strong>Tournaments</strong></ul>
{{#each tournament in model.tournaments}}
<li>
{{tournament.name}}
</li>
{{/each}}
</dl>
router.js
import Ember from "ember";
import config from "./config/environment";
var Router = Ember.Router.extend({
location: config.locationType
});
Router.map(function() {
this.resource("packages", function() {
this.resource("package", {path: "/:package_id"}); });
});
export default Router;
routes/package.js
import Ember from 'ember';
export default Ember.Route.extend({
model: function(params) {
return this.store.find('package', params.package_id);
},
renderTemplate: function() {
this.render();
this.render('packages/index', {
into: 'application',
outlet: 'sidebar'
});
}
});
routes/packages/index.js
import Ember from 'ember';
export default Ember.Route.extend({
model: function(){
return this.store.find('package');
}
});
models/package.js
import DS from 'ember-data';
export default DS.Model.extend({
name: DS.attr('string'),
description: DS.attr('string'),
tournaments: DS.hasMany('tournament',{
async: true,
inverse: 'tournamentPackage'
})
});
models/tournament.js import DS from 'ember-data';
export default DS.Model.extend({
name: DS.attr('string'),
buyIn: DS.attr('string'),
attempts: DS.attr('string'),
description: DS.attr('string'),
tournamentPackage: DS.belongsTo('package', {
async: true
})
});
If I comment out {{outlet 'sidebar'}} and visit a package route directly and refresh, it works fine. I'm sure that on reload it's trying to loop through my singular model from the package route in the sidebar. Other threads have mentioned not to return multiple models on dynamic segments so I'm not sure what to do.
Side note: I'm trying to avoid using controllers as I've heard that Ember will move that logic to other pieces of the framework, but if the solution is to use controllers I have no issue using one.
tournaments
ishasMany
relationship inPackage
? – Kuba Niechciałindex
template, but can you add it? – Kuba Niechciał