Normally rails would automatically render the file corresponding to the controller and action. For example an action index
in TestController
could render the file app/views/test/index.html.erb
. Is there a dynamic way where I can get the path of the corresponding action view file? For example, if I have test#something
I want to get app/views/test/something.html.erb
.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
It is possible. Check out LookupContext and view_renderer
.
Within controller's action:
lookup_context.find_template("#{controller_path}/#{action_name}").identifier
Within view:
@view_renderer.lookup_context.find_template(@virtual_path).identifier