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: http://stackoverflow.com/questions/2622219/rails-layout-name-inside-view
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:
layout = controller.send(:_layout)
if layout.instance_of? String
I think it should be in core, but for now you can make a helper method:
it will return currently used layout name
For Rails 4:
For Rails 3.2:
controller.send(:_layout).identifier returns the fullpath:
You can do what I've done in my Ajax gem for Rails which is to wrap the
def _render_layout_with_tracking(layout, locals, &block)
_render_layout_without_tracking(layout, locals, &block)
alias_method_chain :_render_layout, :tracking
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.