0

Inside an application we allow users to create new records, related to an existing record. To achieve this, we use actions something like this:

createUser() {
  var route = this;

  var model = this.store.createRecord('user', {
    client: route.modelFor('client'),
  });

  route.transitionTo('user.update', model);
 },

The user.update route renders a user-form component, using the model that was passed in the transition. The same route is also used to update existing users.

The issue with this approach is as follows; when refreshing the page, the page errors because the route fails to find the respective record when querying the store (at this point, the URL is /users/null/update). Ideally I'd pass the client (or client.id) argument in the URL so that:

  1. The page can be reloaded without issue.
  2. The client associated with the user is set correctly.

How can I achieve this in Ember.js? I know that this can easily be done using nested routes (by nesting the user.update route inside a client route), but this doesn't make sense visually.

The relevant parts of the router are as follows:

this.route('clients');
this.route('client', {path: 'clients/:id'}, function() {
  this.route('users');
});

this.route('user', {path: 'users/:id'}, function() {
  this.route('update');
});

All I do in the user/update.hbs template is {{user-form user=model}}

joelcox
  • 562
  • 9
  • 19

1 Answers1

0

The problem is that the model you just created has no id at that point because it is not saved, ember can´t route to a model without an id, if possible save the model before you try to transition to the route, if you don´t want to save the model because the user can cancel the action check this thread where a user had the same problem (if I understand you problem correctly), I provided a solution for that problem that I´m using in my own project

https://stackoverflow.com/a/33107273/2214998

Community
  • 1
  • 1