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
.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With