|
Post by fnllc on Sept 2, 2017 22:44:58 GMT
Is it possible to get the current controller name and action name with the Javascript API or using ruby in an ERB?
|
|
|
Post by Vladimir Musulainen on Sept 3, 2017 17:59:09 GMT
Could you, pls, describe your scenario where it is required.
|
|
|
Post by fnllc on Sept 4, 2017 20:50:56 GMT
I'm trying to use Google Analytics and record "Screen Views". I know I can manually set a variable in all my actions and pass it to the erb and Google Analytics code. However, I'm trying to avoid duplicating work if it is already available. For example, in Rails, there is controller.action_name for example, or event controller_name and action_name available in .erb.
|
|
|
Post by Alex Epifanov on Sept 7, 2017 12:11:52 GMT
|
|
|
Post by jontara on Sept 11, 2017 18:51:56 GMT
In Javascript, you can just use document.location, and parse-out the parts of the path with a regex.
In Ruby, the parts are already broken-out for you in @request, which you can use in your controller or in ERBs as you see fit.
@request['request-uri'] @request['query'] @request['action'] @request['model']
I typically add a json-encoded data- attribute in layout.erb with these parts as a convenience. If you use jQuery, jQuery will parse the JSON and it's available (for example) in $('body').data('action') (if you placed it on body).
|
|