Rails3在视图中获取当前布局名称 [英] Rails3 get current layout name inside view

查看:71
本文介绍了Rails3在视图中获取当前布局名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Rails 2.X的答案,但没有Rails 3的答案.如何读取视图中呈现的当前布局的名称.

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.

我的Rails2问题:在视图内滚动布局名称

My Rails2 question: Rails Layout name inside view

谢谢.

推荐答案

在Rails 3.2中使用它要比前面概述的要复杂一些.如果您的控制器显式声明了布局,则controller.send(:_layout)的结果为String,否则为ActionView :: Template.试试这个:

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:

module ApplicationHelper
  def current_layout
    layout = controller.send(:_layout)
    if layout.instance_of? String
      layout
    else
      File.basename(layout.identifier).split('.').first
    end
  end
end

这篇关于Rails3在视图中获取当前布局名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆