How to use slugs in URL?

I am using the latest version of Ember/Ember-cli/Ember-Data (1.13.x)

My data source is a JSON API service (rails-api)

I would like to have URLs like /products/:product_name instead of /products/1

I’ve searched everywhere and tried everything (changing primaryKey, normalizing name to id) but nothing works - seems like something has changed in the recent versions of Ember.

When I enter the URL manually into the browser, Ember creates a new record with ID of NAME (sorry, can’t post a screenshot as I am new here)

Thank you for your help!

1 Like

Does this work for you?

Thank you for the link. But unfortunately no, it doesn’t work.

I’ve moved on to working on other parts of my app for now, but I can’t imagine why this isn’t documented properly (this can be accomplished using AngularJS in a couple lines of code).

//rant off

I needed to know this as well and now that I am up to this part of my application, I did a little more reading and I think I figured it out:

The model hook can take an argument, which is actually an object that contains your URL params. Assuming that you are using Ember CLI:

// app/router.js
Router.map(function () {
  this.route("foo", {
    path: "/foo/:bar"
  });
});

// app/routes/foo.js
export default Ember.Route.extend({
  model: function (params) {
    console.log(params);
  }
});

Now when you navigate to localhost:4200/foo/test, you will see this in the console: Object { bar: "test" }.

This is the equivalent of passing $routeParams to your controller in AngularJS or $stateParams if you are using ui-router. Now you should be able to find your model using variable URL params.

Edit: I missed that your question is regarding Ember Data, so this probably isn’t the answer that you’re looking for.