我可以做request.path_parameters ['controller']和request.path_parameters ['action'],但是有什么像request.path_parameters ['template'],所以我可以辨别哪个模板文件(如index.html。 erb)正在渲染?在Rails中是否有一个变量等同于正在呈现的模板?
我正在写自动套在身上ID为模板的方法被呈现,便于操控的CSS:
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
...
谢谢,samuil。但params [:action]不能正常工作,因为虽然有时“show”动作会呈现“show”页面,但有时“update”动作也会呈现“show”页面。我需要一种方法将
明确设置为所呈现模板的名称(不管操作如何)。 “content_for”是一种解决方法,它使我能够在每个模板文件中显式设置主体ID,但需要在创建的每个模板文件中重复插入该行。 – 2010-06-03 22:59:55我有一个稍微不同的情况下,在我的情况下,有几个theme_paths添加到模板解析器,并基于模板可用性模板从主题1或主题2等摘取,我想知道它在哪里是theme2/theme1 – 2016-01-05 04:41:15