# How would you simplify these nested routes so there isn't duplicated code?

**URL:** <https://discuss.emberjs.com/t/how-would-you-simplify-these-nested-routes-so-there-isnt-duplicated-code/13691>\
**Category:** Learning Team\
**Created:** [October 2, 2017, 9:38am UTC](https://discuss.emberjs.com/t/how-would-you-simplify-these-nested-routes-so-there-isnt-duplicated-code/13691 "2017-10-02T09:38:49Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnnyicon](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/johnnyicon/32/13390_2.png) [@johnnyicon](https://discuss.emberjs.com/u/johnnyicon)\
**Post date:** [October 2, 2017, 9:38am UTC](https://discuss.emberjs.com/t/how-would-you-simplify-these-nested-routes-so-there-isnt-duplicated-code/13691/1 "2017-10-02T09:38:49Z")

</div>

I’m confused as to how nested routes and dynamic segments work with Ember.

Below is how I’ve defined a series of nested routes for a House. I’ve been following the pattern for defining routes using Dynamic Models guide found here: [Specifying a Route's Model - Routing - Ember Guides](https://guides.emberjs.com/v2.15.0/routing/specifying-a-routes-model/#toc_dynamic-models)

```
this.route('houses');
this.route('house', {path: 'houses/:house_id'}, function() {
  this.route('show', {path: 'houses/:house_id/show'},);
  this.route('level-1', function() {
    this.route('master-bedroom', {path: 'houses/:house_id/level-1/master-bedroom'});
    this.route('bathroom', {path: 'houses/:house_id/level-1/bathroom'});
    this.route('patio', {path: 'houses/:house_id/level-1/patio'});
  });
});

```

I don’t understand why I have to keep specifying the entire path in order to get access to the `:house_id` dynamic segment. If I don’t specify the entire path with the dynamic segment `:house_id`, then trying to access the `:house_id` in the `params` object in the `model()` hook returns `undefined`.

Is there a better way to do this?

---

<div class="post-metadata">

**Author:** ![locks](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/locks/32/3878_2.png) [@locks](https://discuss.emberjs.com/u/locks)\
**Post date:** [October 2, 2017, 10:48am UTC](https://discuss.emberjs.com/t/how-would-you-simplify-these-nested-routes-so-there-isnt-duplicated-code/13691/2 "2017-10-02T10:48:01Z")

</div>

Hi @johnnyicon, it seems like there are various concepts being mixed up here. According to the `router` you posted here, the path for `house.level-1.bathroom` is `/houses/:house_id/level-1/houses/:house_id/level-1/bathroom`.

The `house.show` route also jumps out. Do you really want a `/show` route, or would you be better served by the `house.index` route?

---

<div class="post-metadata">

**Author:** ![jkarsrud](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/jkarsrud/32/15362_2.png) [@jkarsrud](https://discuss.emberjs.com/u/jkarsrud)\
**Post date:** [October 2, 2017, 10:51am UTC](https://discuss.emberjs.com/t/how-would-you-simplify-these-nested-routes-so-there-isnt-duplicated-code/13691/3 "2017-10-02T10:51:56Z")

</div>

To get the the `:house_id` param from the parent routes, you can use `paramsFor`, in your case `paramsFor(‘house’). The guides mention this in a section further down on the page you linked to: [Specifying a Route's Model - Routing - Ember Guides](https://guides.emberjs.com/v2.15.0/routing/specifying-a-routes-model/#toc_reusing-route-context). That will fix your issue with have to duplicate the paths. As @locks mentions, when you’re duplicating them like you have, you will also end up with a path that might not be what you expect it to be.

---

<div class="post-metadata">

**Author:** ![knownasilya](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/knownasilya/32/12912_2.png) [@knownasilya](https://discuss.emberjs.com/u/knownasilya)\
**Post date:** [October 2, 2017, 2:08pm UTC](https://discuss.emberjs.com/t/how-would-you-simplify-these-nested-routes-so-there-isnt-duplicated-code/13691/4 "2017-10-02T14:08:49Z")

</div>

Also this is a great resource for figuring out routes: [ember-diagonal](http://alexspeller.com/ember-diagonal)

You can paste your route config and see the resulting urls for each route.

---

<div class="post-metadata">

**Author:** ![johnnyicon](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/johnnyicon/32/13390_2.png) [@johnnyicon](https://discuss.emberjs.com/u/johnnyicon)\
**Post date:** [October 3, 2017, 3:23am UTC](https://discuss.emberjs.com/t/how-would-you-simplify-these-nested-routes-so-there-isnt-duplicated-code/13691/5 "2017-10-03T03:23:56Z")

</div>

Thanks for your response, locks. The example above was only to illustrate that I was having to use the `{path: ''}` on every router entry.

---

<div class="post-metadata">

**Author:** ![johnnyicon](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/johnnyicon/32/13390_2.png) [@johnnyicon](https://discuss.emberjs.com/u/johnnyicon)\
**Post date:** [October 3, 2017, 3:24am UTC](https://discuss.emberjs.com/t/how-would-you-simplify-these-nested-routes-so-there-isnt-duplicated-code/13691/6 "2017-10-03T03:24:29Z")

</div>

Thank you! This is exactly what I was looking for! I opted to use `modelFor` in my case as it saved me a step. Cheers!

---

<div class="post-metadata">

**Author:** ![johnnyicon](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/johnnyicon/32/13390_2.png) [@johnnyicon](https://discuss.emberjs.com/u/johnnyicon)\
**Post date:** [October 3, 2017, 3:25am UTC](https://discuss.emberjs.com/t/how-would-you-simplify-these-nested-routes-so-there-isnt-duplicated-code/13691/7 "2017-10-03T03:25:53Z")

</div>

Cheers! This is definitely useful! (One wonders why this isn’t part of the Ember Inspector… 🤔)

---

<div class="post-metadata">

**Author:** ![knownasilya](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/knownasilya/32/12912_2.png) [@knownasilya](https://discuss.emberjs.com/u/knownasilya)\
**Post date:** [October 4, 2017, 8:10pm UTC](https://discuss.emberjs.com/t/how-would-you-simplify-these-nested-routes-so-there-isnt-duplicated-code/13691/8 "2017-10-04T20:10:38Z")

</div>

> [@johnnyicon](#):
>
> One wonders why this isn’t part of the Ember Inspector… 🤔

Because you haven’t added it yet 😉
