Search code examples
ember.jsember-dataember-cli

Go to new item route after creation in Emberjs


I've something like this in my route:

actions: {
  save(title, description) {
    const newCard = this.get('store').createRecord('card', { title, description } );
    newCard.save().then((card) => {
      // card.id contains the id of the new card
      this.transitionTo('cards.all'); // problem with this line
    });
  }
}

I want to go to the new card route after creating it. I can get the id of the new item from card.id. I've tried the following but of no use:

this.transitionTo('cards.card', card.id);

This throws some error since the cards.card route can't find the id in the params.

The following:

this.transitionTo('cards/' + card.id);

throws some error stating that cards/45 route not found but if the new item is created and I can navigate to it.

I'm using Ember 2.6.1.

EDIT

My router.js file:

this.route('cards', function() {
  this.route('all');
  this.route('card', {path: '/:card_id'}, function() {
    this.route('edit');
  });
  this.route('new');
});

Solution

  • It should be this:

    this.transitionTo('cards.card', card);
    

    I only needed to do this since my card.js was expecting params object and I was trying to pass a number:

    model(params) {
      return this.store.find('card', params.id);
    }