【问题标题】:Rails 3: How to get the name of the current layout?Rails 3:如何获取当前布局的名称?
【发布时间】:2011-12-27 22:21:21
【问题描述】:

在我的控制器中,我有:

render :layout => 'mobile'

在我看来,我希望能够获得布局的名称,在本例中为“mobile”。

我不需要控制器或动作名称...只是布局的名称。

我使用的是 Rails 3.0.6。

【问题讨论】:

标签: ruby-on-rails ruby-on-rails-3


【解决方案1】:

我迟到了,但试试这个:

controller.send(:_layout).virtual_path.name

在 Rails 3.2 中测试

【讨论】:

    【解决方案2】:

    为什么不在控制器中创建变量:

    @layout = 'mobile'
    render :layout => @layout
    

    那么你就可以在你的视图中访问@layout

    【讨论】:

    • 因为有更好的方法来做到这一点:controller.send :_layout.
    • @RyanBigg 这对我不起作用。在我的示例中,使用:_layout 输出application,而不是mobile
    • 只要您不手动渲染这样的布局,它就可以工作:render :layout => 'mobile'
    猜你喜欢
    • 1970-01-01
    • 2013-09-02
    • 2011-03-20
    • 2017-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-02
    • 1970-01-01
    相关资源
    最近更新 更多