I have a scenario where I would like to have the name of the view that will be rendering while I'm in the layout file. I can find solutions to find which layout will be wrapping the current view from the view, but not the other way around. How can I find which view is rendering?
相关问题
- Question marks after images and js/css files in ra
- Using :remote => true with hover event
- Eager-loading association count with Arel (Rails 3
- Google Test - generate values for template class i
- Template Specialization for Private Types
相关文章
- Right way to deploy Rails + Puma + Postgres app to
- AWS S3 in rails - how to set the s3_signature_vers
- how to call a active record named scope with a str
- How to add a JSON column in MySQL with Rails 5 Mig
- “No explicit conversion of Symbol into String” for
- form_for wrong number of arguments in rails 4
- Rspec controller error expecting <“index”> but
- Factory_girl has_one relation with validates_prese
I like the following approach, as you can reduce code size a good bit in a lot of cases. Include this in your application_controller.rb:
Then, if your views correspond to an action, you just do:
In Rails 3.0.3, I was able to see the name of the controller and action using controller_name and action_name. But those are not publicly documented (at least the action name) so I wouldn't depend on it long-term.
It might be better to monkey patch template render. In an initializer:
Then use @last_template in your layout.
The following solution works in Rails 3.1. Place this code in an initializer. (The rails 3.0.3 answer is not working any more in Rails 3.1)
This enables an @active_template variable for every controller. This is an instance of an ActionViewTemplate class.
The method active_template_virtual_path method returns the template as a name in the following form "controller/action"