Question

I'm working on a simple todo app where each todo item belongs to a user. I'm getting this error:

Uncaught Error: Nothing handled the action 'createTodo'.

I think I'm missing a route and maybe a controller, but I'm not really sure what I need to do.

app/router.js:

import Ember from 'ember';

var Router = Ember.Router.extend({
  location: TodoENV.locationType
});

Router.map(function() {
  this.route('about');
  this.resource('users', function() {
    this.route('show', {path: ':user_id'});
  });
});

export default Router;

app/routes/users/index.js:

import Ember from 'ember';

export default Ember.Route.extend({
  model: function() {
    return this.store.find('user');
  }
});

app/models/user.js:

import DS from 'ember-data';

export default DS.Model.extend({
  name: DS.attr('string'),
  todos: DS.hasMany('todo')
});

app/models/todo.js:

import DS from 'ember-data';

export default DS.Model.extend({
  title: DS.attr('string'),
  user: DS.belongsTo('user')
});

app/controllers/todo.js:

import Ember from 'ember';

export default Ember.ArrayController.extend({
  actions: {
    createTodo: function() {
      var title = this.get('newTitle');
      if (!title.trim()) { return; }

      var todo = this.store.createRecord('todo', {
        title: title // how do I get the user id?
      });

      this.set('newTitle', '');

      todo.save();
    }
  }
});

app/templates/users/show.hbs:

<h4>{{name}}</h4>

<h5>Todos</h5>
{{input type="text" id="new-todo" placeholder="new todo"
      value=newTitle action="createTodo"}}
<ul>
  {{#each todos}}
    <li>{{title}}</li>
  {{/each}}
</ul>
Was it helpful?

Solution

The problem is createTodo is implemented in TodoController whereas you are using createTodo action in users/show template. Action is sent to the UsersShowController where createTodo is not implemented. Move createTodo action into UsersShowController and everything should be OK.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top