kchak
kchak

Reputation: 8052

Why does route to home changes after rendering a template?

I am just getting started using the iron:router package. These are my project files:

router-example.js

    if (Meteor.isClient) {
    //some code
    }
    if (Meteor.isServer) {
   //some code
    }
    Router.route('/', function(){
        this.render('Home');
    });
    Router.route('/hello', function(){
        this.render('hello');
    });
    Router.route('/items', function(){
        this.render('Items');
    });
    Router.route('/serverItem', function(){
        var req = this.request;
        var res = this.response;
        res.end('Hello from the server\n');
    }, {where: 'server'});

router-example.html

<body>
<h1>Welcome to Meteor!</h1>
<ol>
    <li><a href="{{pathFor '/'}}">This routing doesn't work</a></li>
    <li><a href="{{pathFor 'hello'}}">Hello Template</a></li>
    <li><a href="{{pathFor 'items'}}">Items Template</a></li>
    <li><a href="{{pathFor 'serverItem'}}">Server Item</a></li>
    <li><a href="http://localhost:3000/">Hard link works</a></li>
</ol>
</body>

templates.html

<template name = "Home">
<h2>Default: '/' brings you here</h2>
<p>This is the home template</p>
</template>

<template name = "Items">
<h2>This is the items template. Items Page linked using pathFor helper</h2>
</template>

<template name="hello">
<button>Click Me</button>
<p>You've pressed the button {{counter}} times.</p>
</template>

So at the home page "localhost:3000", the "Home" template is rendered by default, as expected. Once I click on the other links: Hello Template, Items Template etc. Those are rendered, but home link specified using the {{pathFor '/'}} helper stops working and I have to use a hard link (localhost:3000) to get back to the home page. Hovering the mouse over that link shows that it's pointing to a different route.

So what am I doing wrong here?

Upvotes: 0

Views: 73

Answers (1)

Marius Darila
Marius Darila

Reputation: 873

You can specify route name in order to use {{pathFor 'routeName'}}:

Router.route('/', { name: 'home', template: 'Home' })

Look here for full example https://github.com/iron-meteor/iron-router/blob/devel/Guide.md#route-specific-options

If no name is provided, the router guesses a name based on the path

Upvotes: 1

Related Questions