获取对应控制器动作视图文件的路径 [英] Get path of corresponding controller action view file
本文介绍了获取对应控制器动作视图文件的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
通常 rails 会自动渲染对应于控制器和动作的文件.例如,TestController
中的操作 index
可以渲染文件 app/views/test/index.html.erb
.有没有一种动态的方式可以获取对应的动作视图文件的路径?比如如果我有test#something
我想获取app/视图/测试/something.html.erb
.
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
.
推荐答案
这是可能的.查看 LookupContext 和 view_renderer
.
It is possible. Check out LookupContext and view_renderer
.
在控制器的动作中:
lookup_context.find_template("#{controller_path}/#{action_name}").identifier
在视图中:
@view_renderer.lookup_context.find_template(@virtual_path).identifier
这篇关于获取对应控制器动作视图文件的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文