Search code examples
ember.jsember-dataember-cli

Ember route with dynamic segment name


I'm just starting with Ember JS and Ember CLI and trying to figure out this routing issue. I have a group model that has many game models. With the following route, I am able to display games just fine from a group URL:

Router.map(function() {
  this.resource("groups", function() {
    this.route('show', {path: ':group_id/show' });
  });
});

This results in a URL with the form:

http://localhost:4200/groups/1/show

Suppose one of the group names is "wizards". I'd like to to be able to construct a URL in the following form and render all the games that belong to "wizards":

 http://localhost:4200/wizards

Any tips are appreciated.


Solution

  • Like @blessenm points out in the comments, your router would change from

    Router.map(function() {
      this.resource("groups", function() {
        this.route('show', {path: ':group_id/show' });
      });
    });
    

    to

    Router.map(function() {
      this.resource("group", { path: ':group_name'});
    });
    

    The second parameter to this.resource() or this.route() is optional. If you don't pass anything in - it assumes the same name as your route/resource (groups, in your case). If you pass in an object that has a path: key - you are specifying what the url to the route is, including a dynamic segment. See here for Ember documentation on this.