# Passing a relationship model through link-to helper?

**URL:** <https://discuss.emberjs.com/t/passing-a-relationship-model-through-link-to-helper/9084>\
**Category:** Ember CLI\
**Created:** [November 10, 2015, 7:07pm UTC](https://discuss.emberjs.com/t/passing-a-relationship-model-through-link-to-helper/9084 "2015-11-10T19:07:59Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![JMendoza](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/jmendoza/32/11672_2.png) [@JMendoza](https://discuss.emberjs.com/u/JMendoza)\
**Post date:** [November 10, 2015, 7:07pm UTC](https://discuss.emberjs.com/t/passing-a-relationship-model-through-link-to-helper/9084/1 "2015-11-10T19:07:59Z")

</div>

Heyy!!

I’m having trouble passing a model to a route in ember cli. I’m making a simple app where posts have and author and a title. When you click the title you go to the post details and when you click the author you go to the author’s profile. My problem is that I go to the respective user but when I refresh the page I get a n error in the author route. I have no idea why, I’m guessing it has to do with the model not being fetched again when I refresh since it passes the model using link-to helper

My code:

#### app/models/author.js

```
import DS from 'ember-data';

export default DS.Model.extend({
  posts: DS.hasMany('post', {async: true}),
  name: DS.attr('string'),
  url: DS.attr('string')
});

```

#### app/models/post.js

```
import DS from 'ember-data';

var attr= DS.attr;

export default DS.Model.extend({
 author: DS.belongsTo('author'),
 title: attr('string'),
 description: attr('string'),
 date: attr('date'),
 url:attr('string'),
});

```

#### app/routes/author.js

```
import Ember from 'ember';

export default Ember.Route.extend({
   setupController: function(controller, model) {
   model.reload();       
   controller.set('model', model);}
 });

```

#### app/templates/posts.hbs\</h4? {{#each model as |post|}} ``` <div class="media"> <a class="pull-left" > <img class="media-object" src={{post.url}} style="width:200px;height:200px"> </a> <div class="media-body"> <h1>{{#link-to 'post' post}}{{post.title}}{{/link-to}}</h1> <h4>Posted by: {{#link-to 'author' post.author.id}}{{post.author.name}}{{/link-to}} </h4> <p>{{post.description}}</p> </div> </div> ``` {{/each}}

---

<div class="post-metadata">

**Author:** ![vikram\_s\_narayan](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/vikram_s_narayan/32/11115_2.png) [@vikram\_s\_narayan](https://discuss.emberjs.com/u/vikram_s_narayan)\
**Post date:** [November 11, 2015, 2:59am UTC](https://discuss.emberjs.com/t/passing-a-relationship-model-through-link-to-helper/9084/2 "2015-11-11T02:59:49Z")

</div>

In your route, you can get rid of the setupController hook and do something like:

```
import Ember from 'ember';

export default Ember.Route.extend({
   model: function(params){
        return this.store.find('author', params.author_id);
    }
 });

```

And then in your router.js file, setup the route like this:

```
//boilerplate code generated by Ember-CLI
Router.map(function() {
  this.route('author', {path: ':author_id'});
  //other routes  
});
//boilerplate code generated by Ember-CLI

```

---

<div class="post-metadata">

**Author:** ![JMendoza](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/jmendoza/32/11672_2.png) [@JMendoza](https://discuss.emberjs.com/u/JMendoza)\
**Post date:** [November 11, 2015, 3:07am UTC](https://discuss.emberjs.com/t/passing-a-relationship-model-through-link-to-helper/9084/3 "2015-11-11T03:07:59Z")

</div>

Hi, thank you for your response. I tried that but I still get the error when I refresh the page, this is the error: Error while processing route: author Unexpected token \< SyntaxError: Unexpected token \<…

Do you have any ideas? thanks

---

<div class="post-metadata">

**Author:** ![vikram\_s\_narayan](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/vikram_s_narayan/32/11115_2.png) [@vikram\_s\_narayan](https://discuss.emberjs.com/u/vikram_s_narayan)\
**Post date:** [November 11, 2015, 7:07am UTC](https://discuss.emberjs.com/t/passing-a-relationship-model-through-link-to-helper/9084/4 "2015-11-11T07:07:48Z")

</div>

can you please change "this.route(‘author’, {path: ‘:author\_id’}); " to this.route(‘author’, {path: ‘/:author\_id’});

---

<div class="post-metadata">

**Author:** ![JMendoza](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/jmendoza/32/11672_2.png) [@JMendoza](https://discuss.emberjs.com/u/JMendoza)\
**Post date:** [November 11, 2015, 7:02pm UTC](https://discuss.emberjs.com/t/passing-a-relationship-model-through-link-to-helper/9084/5 "2015-11-11T19:02:48Z")

</div>

Tried it…I don’t now if this helps but his is my server side, it displays the right profile but the problem is when I refresh the page. …

```
var authors=[];//list of authors
var profileRouter= express.Router();

profileRouter.get('/', function(req, res) {
res.send({
'authors':authors
 });
});

profileRouter.get('/:id', function(req, res) {
res.send({
'author': authors.find(function(user){
  return author.id==req.params.id
  // id: req.params.id,
})
});
});

app.use('/api/author', profileRouter);

```

---

<div class="post-metadata">

**Author:** ![broerse](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/broerse/32/9497_2.png) [@broerse](https://discuss.emberjs.com/u/broerse)\
**Post date:** [November 12, 2015, 6:01pm UTC](https://discuss.emberjs.com/t/passing-a-relationship-model-through-link-to-helper/9084/6 "2015-11-12T18:01:07Z")

</div>

A working example [http://bloggr.exmer.com/](http://bloggr.exmer.com/). I still use setupController see source:

> <https://github.com/broerse/ember-cli-blog/blob/master/app/routes/posts.js>

---

<div class="post-metadata">

**Author:** ![JMendoza](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/jmendoza/32/11672_2.png) [@JMendoza](https://discuss.emberjs.com/u/JMendoza)\
**Post date:** [November 12, 2015, 6:43pm UTC](https://discuss.emberjs.com/t/passing-a-relationship-model-through-link-to-helper/9084/7 "2015-11-12T18:43:36Z")

</div>

Thank you all, It worked, it was something in my server side 😄

---

<div class="post-metadata">

**Author:** ![eibrahim](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/eibrahim/32/15648_2.png) [@eibrahim](https://discuss.emberjs.com/u/eibrahim)\
**Post date:** [November 17, 2015, 1:45pm UTC](https://discuss.emberjs.com/t/passing-a-relationship-model-through-link-to-helper/9084/8 "2015-11-17T13:45:46Z")

</div>

By the way, you don’t need to pass the id, you can pass the object and ember is smart enough to use the id. So instead of

```
{{#link-to 'author' post.author.id}}

```

you can do this

```
{{#link-to 'author' post.author}}

```
