Get path of corresponding controller action view f

2020-07-14 09:47发布

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条回答
beautiful°
2楼-- · 2020-07-14 10:40

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
查看更多
登录 后发表回答