Rails 3.2 引擎布局 [英] Rails 3.2 Engine Layouts

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

问题描述

我正在努力理解 Rails 3.2 在使用可安装引擎时如何应用布局.

I'm struggling to understand how Rails 3.2 applies layouts when using mountable engines.

场景:我正在构建一个引擎,它本身具有用于各种管理功能的仪表板视图和管理视图.我希望仪表板的布局可以被基础应用程序覆盖(如果用户需要),但管理员应该始终使用自己的布局.

Scenario: I'm building an engine which itself has a dashboard view and an admin view for various admin functions. I want the dashboard to have its layout overridable by the base application (if the user desires) but the admin should always use its own layout.

这是我目前在引擎中的内容;

Here's what I have at the moment inside my engine;

application_controller.rb

application_controller.rb

module Myengine
  class ApplicationController < ActionController::Base

admin/dashboard_controller.rb

admin/dashboard_controller.rb

module Myengine                                                                                                          
  class Admin::DashboardController < ApplicationController

现在我的引擎 application.html.erb 应用了可怕的红色背景,而基础应用程序 application.html.erb 使用了令人愉悦的黄色背景,因此我可以区分正在应用的应用程序布局.

now I have my engines application.html.erb apply a hideous Red background whilst the base applications application.html.erb uses a pleasant yellow background so I can distinguish which application layout is being applied.

在这种情况下,如果我首先访问基础应用程序,我会看到我的黄色背景(来自基础应用程序),如果我同时访问引擎和引擎管理路径,黄色背景仍然存在.

In this situation, if I access the base application first I see my yellow background (from the base app) and if I go to both the engine and the engines admin path the yellow background remains.

如果我重新启动服务器并首先访问引擎,那么我会看到引擎和引擎管理路径的红色背景,而基础应用程序显示黄色背景.

If I restart the server and access the engine first then I see the red background for the engine and the engines admin path whilst the base application shows the yellow background.

如果我修改我的 admin/dashboard_controller.rb 如下;

If I modify my admin/dashboard_controller.rb as follows;

module Myengine
  class Admin::DashboardController < ApplicationController
    layout 'myengine/application'

我希望它只适用于引擎/管理控制器 - 但如果我重新启动服务器并访问引擎/管理路径,我会看到红色背景,而引擎的根视图使用基本应用程序黄色布局.

which I would expect to only apply to the engine/admin controller - but if I restart the server and access the engine/admin path I see the red background whilst the root view of the engine uses the base application yellow layout.

如果我再次重新启动服务器并访问已安装引擎的根目录,则会应用红色背景,该背景也保留在引擎管理路径上.

If I restart the server again and access the root of the mounted engine I get the red background applied which remains on the engines admin path too.

Aaaaarggggghhhhh!

Aaaaarggggghhhhh!

根据首先访问应用程序的哪个路径,使用不同的应用程序布局是否是预期行为?确定不是??我一定是做错了什么!

Is it expected behaviour to have different layouts of the application used depending on which path of the application is accessed first? Surely not?? I must be doing something wrong!

推荐答案

我已经调试了问题,实际上它不是引擎中的错误.问题是由 Rails 依赖项的加载方式引起的.

I've debugged the problem and actually it's not a bug in Engines. The problem is caused by the way rails dependencies are loaded.

此代码在您展示的两种情况下的行为会有所不同:

This code will behave differently in 2 scenarios that you're showing:

module Enginedemo
  class DashboardController < ApplicationController
  end
end

如果 ApplicationController 已经加载,rails 会假设我们只是想使用它,你实际上不是从 Enginedemo::ApplicationController 继承,而是从 ApplicationController.在另一种情况下,当您第一次加载引擎的控制器时,ApplicationController 尚未加载,因此 Rails 做了正确的事情.

If ApplicationController is already loaded, rails will assume that we just want to use it and you will actually not inherit from Enginedemo::ApplicationController but from ApplicationController. In the other scenario, when you first load engine's controller, ApplicationController is not loaded yet, so Rails does the right thing.

幸运的是,此问题仅发生在开发环境中,因为在应用程序启动时加载生产控制器.

Thankfully this problem occurs only in development environment as in production controllers are loaded when application is booting.

我不确定这是否可以在 rails 依赖项中轻松修复,我会看一下.

I'm not sure if this is something that can be easily fixed in rails dependencies, I will take a look at it.

现在,请明确要求应用程序控制器:

For now, please explicitly require application controller:

require 'enginedemo/application_controller'

module Enginedemo
  class DashboardController < ApplicationController
  end
end

这篇关于Rails 3.2 引擎布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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