RunnerRick
RunnerRick

Reputation: 969

Sails.js: How To Access Data in Server-Side EJS View for Single Model

The Sails.js Wiki clearly describes how to loop through an array of models and generate HTML. See https://github.com/balderdashy/sails/wiki/views. However it is not clear to me how to get at the model for a details view.

Given a model generated via the shell command: sails generate user, there will be these routes:

/user
/user/:id

(Here :id is a token replaced with the unique id of the model. For more details see http://sailsjs.org/#documentation/routes.)

The wiki's example handles the first route, /user, but what happens when you go to /user/:id--how do you get at the model from the EJS view?

I would expect something like <%= model.propertyName %> would work.

Bare in mind I'm asking about generating the HTML on the server. (I know how to get the data and render client-side.)

Upvotes: 3

Views: 7392

Answers (1)

ataman
ataman

Reputation: 2664

You can create controller action that matches the route and manually pass found model to the view.

In UserController:

find: function(req, res) {
    User.findOne({'id': req.params['id']}, function(err, user) {
        res.view({user: user})
    })
}

Then You can reference this model in views/user/find.ejs:

<%- user.id %>
<%- user.name %>

Upvotes: 10

Related Questions