I can do request.path_parameters[‘controller’] and request.path_parameters[‘action’], but is there anything like request.path_parameters[‘template’] so I can discern which template file (such as index.html.erb) is being rendered?
I’m writing a method that automatically sets the body id to the template being rendered, for easy css manipulation:
class ApplicationController < ActionController::Base
...
after_filter :define_body_selector
...
def define_body_selector
# sets @body_id to the name of the template that will be rendered
# ie. if users/index.html.erb was just rendered, @body_id gets set to "index"
@body_id = ???
end
...
If you have different template files you could use
content_forin them (check out guide about layouts and templates) to set id in layout file, or just stick toparams[:action](it should be enough — choice of template is based on action called).You can make universal
before_filterfor all (or not all) actions withAlways think how to keep your code DRY!
EDIT:
You could define hash that will link actions with matching templates:
Within your layout file just add
EDIT:
It is possible to access template via
ActionBase::templatemethod, but it won’t work the way you would like it to. If you callfilenameornamemethod in layout file, you will get path to layout file, not template. AFAIK It is impossible to check what template is being rendered, as multiple of them can be used to render single action.