I have the answer for the Rails 2.X but not for Rails 3. How can I read the name of a current layout rendered inside a view.
My Rails2 question: Rails Layout name inside view
Thx.
I have the answer for the Rails 2.X but not for Rails 3. How can I read the name of a current layout rendered inside a view.
My Rails2 question: Rails Layout name inside view
Thx.
All the approaches in the previous answers try to guess the name via private methods, but there's no need to guess and can be easily accomplished with the public API:
Override in any controller that won't use the standard layout:
And now in your views:
in rails 5
This works for me:
I have used in Rails4 at view pages and got reuslt.
I hope this help.
You can do what I've done in my Ajax gem for Rails which is to wrap the
_render_layout
method:Then you can access the value that was set from your view (I'm pretty sure you have access to the controller there...) or in your controller in an
after_filter
, which is what I do.I've written a custom RSpec 2 matcher which can be used to test layout rendering in Rails 3.
For rails 5:
This does NOT work:
Getting this to work in Rails 3.2 is a little more complicated than previously outlined. If your controller explicitly declares a layout, then the result of
controller.send(:_layout)
is a String, but otherwise it's an ActionView::Template. Try this: