how to get current controller and action name? #1031

Closed
askie opened this Issue Oct 25, 2013 · 9 comments

Projects

None yet

7 participants

@askie

I want to get current controller and action name string,
is that a method?

@uhho

Yes, controller name and action are stored in req.target in following format:

req.target = {
    controller : 'XXX',
    action: 'XXX'
}

You can access this variable both on back end and front end.

@askie

thx

@askie askie closed this Oct 25, 2013
@kokujin

This is interesting, how can this value be accessed in the front-end Lukas?

@uhho

@kokujin
You can access it the same way as any other JS variable you're setting in the controller.
The difference is, by default this variable is exposed on the front end (no need to set it via res.view({...})).

For example, let's say you've got a link and you want it to be highlighted only on particular controller/action:
In case of EJS templates it will be:

<a href="#" class="<%= (req.target.action === 'index') ? 'highlighted' : '' %>">Link</a>

Hope that helps!

@randallmeeker

did this go away? I'm using the latest version and don't have target on the req object.

@clarkorz

try req.options.controller and req.options.action. But it seems that they only exist for custom blueprints action and custom routes. So if you have problem, add a route in config/routes.js.

@randallmeeker

That is the issue. If it is a custom action and/or route then I already know my controller / action. I was looking for this because I wanted to do a pre-flight check in a policy that ran before all my blueprint actions. I needed the action being preformed on the model.

@echorohit

How to get the controller and action name for request.referrer in sails. sails.log(req.referrer); gives me controller/action. How to get controller and action name from req.referrer directly??

@luqezman

Hello Guys, news?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment