【问题标题】:Undefined method `layout' for未定义的方法“布局”
【发布时间】:2015-10-22 08:49:37
【问题描述】:

我的class Site::BaseController < ApplicationController有方法

before_filter :check_layout

   def check_layout
    if @user.site_theme == 'hometastic'
      layout 'hometastic'
    else
      layout 'agent'
    end
  end

当我只做时

layout 'agent'  

效果很好

但是当我添加before_filter 时,我得到了undefined method layout for

Rails 3.2.16

有什么建议吗? error screen

【问题讨论】:

  • 你能发布完整的错误吗?
  • layout 更改为self.class.layout 会发生什么?
  • self.class.layout 'hometastic' 作品@Pavan
  • @xxx你试过japed的回答吗

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


【解决方案1】:

您可以使用 Rails 在处理请求时用来评估它的符号。 Guides

layout :themed_layout

def themed_layout
  if @user.site_theme == 'hometastic'
    'hometastic'
  else
    'agent'
  end
end

【讨论】:

  • 或者干脆@user.site_theme == 'hometastic' ? 'hometastic' : 'agent'
  • 是的,如果您更喜欢三元运算符。
  • 你只需要返回theme string ..即'hometastic' or 'agent' 在layout里面调用layout可能会报错,
【解决方案2】:

只需在应用程序控制器中添加以下代码行:

包括 ActionView::Layouts

【讨论】:

  • 但在此之后我收到另一个错误:NoMethodError: undefined method `helper' for ActiveAdmin::Devise::SessionsController:Class
  • 我也有同样的问题,但是在应用程序控制器中修改后只需添加 ::base 意味着我们的应用程序控制器是从 Base "class ApplicationController
  • 也许在这之后你就能解决这个问题了。
  • 你好。欢迎来到 SO。您为什么不编辑您的答案以将您的回复添加到您的答案中?按照回复后似乎可以解决解决方案。
【解决方案3】:

我认为“布局”的中间件未正确加载

要在 Rails 应用程序中加载“布局”中间件, 将下面的行写入您的应用程序控制器

include ::ActionView::Layouts

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-24
    • 2011-09-13
    • 1970-01-01
    • 2015-06-17
    • 2013-07-25
    相关资源
    最近更新 更多