Rails 4 - 当前布局的名称? [英] Rails 4 - Name Of Current Layout?

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

问题描述

我找到了大量关于 Rails 3 的资源,但没有找到关于 Rails 4 的资源:

I've found numerous resources for Rails 3, but none for Rails 4:

为了让事情保持干燥,我们有一个定义一些元标记的方法.我想在 title 参数中包含布局:

In an effort to keep things DRY, we have a method which defines some meta tags. I'd like to include the layout in the title param:

  #app/controllers/application_controller.rb
  before_action :set_meta_tags

  def set_meta_tags
    title = (layout != "application") ? "#{layout} ::" : false
    set_meta title: "#{layout} #{setting(:site, :title)}", description: setting(:site, :description)
  end

唯一的问题是我不知道如何在 Rails 4 中返回当前布局 - 任何帮助将不胜感激!

Only problem is I don't know how to return the current layout in Rails 4 - any help will be greatly appreciated!

推荐答案

在 Rails 5 中,代码为:

In Rails 5, the code is:

controller.send :_layout, ["some_string_here"]

我不知道为什么它需要数组中的一个字符串,但这就是它为我工作的原因.我们的帮助文件如下所示:

I don't know why it needs a string in the array but that's what got it working for me. Our helper file looks as follows:

def current_layout
  layout = controller.send :_layout, ["test"]
  return layout.inspect.split("/").last.gsub(/.haml/,"")
end

这篇关于Rails 4 - 当前布局的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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