【问题标题】:Rendering application.html.erb-like layout for a new namespace为新命名空间呈现类似 application.html.erb 的布局
【发布时间】:2014-03-10 15:02:03
【问题描述】:

我正在为我的应用构建一个自定义管理部分,我希望 :admin 命名空间有自己的布局。

到目前为止,我一直在 app/controllers/application_controller.rb 中玩这个:

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception
  layout :cond_layout

  def cond_layout
    if controller_path.split("/").first == "admin"
      "panel"
    else
      "application"
    end
  end
end

一个朋友告诉我用的,但它闻起来很腥。除了 Semantic-UI 停止在 admin 命名空间中工作。我做错了什么?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 namespaces


    【解决方案1】:

    如果您有并且您可能在 admin 命名空间中也有一些 Admin::MainController.rb?然后毫不犹豫地覆盖那里的布局。恕我直言,干净多了..

    【讨论】:

      【解决方案2】:

      找到一条路,我相信一条干净的路。 定义一个名为foo 的新布局。然后是这样的管理控制器:

      class AdminController < ApplicationController
        layout "foo"
        before_action :bar
      end
      

      现在每个继承自AdminController 的控制器都将呈现foo 布局并执行bar 操作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-02-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-12
        • 1970-01-01
        • 1970-01-01
        • 2013-01-25
        相关资源
        最近更新 更多