Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3.2 Engine Layouts

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

module Myengine
  class ApplicationController < ActionController::Base

admin/dashboard_controller.rb

module Myengine                                                                                                          
  class Admin::DashboardController < ApplicationController

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.

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!

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!

like image 610
John Beynon Avatar asked Apr 06 '12 10:04

John Beynon


People also ask

What are layouts in Rails?

In Rails, layouts are pieces that fit together (for example header, footer, menus, etc) to make a complete view. An application may have as many layouts as you want. Rails use convention over configuration to automatically pair up layouts with respective controllers having same name.

How do you use nested layouts Rails?

The normal Rails app views are presented in a higher-level layout that has a little less markup surrounding it. Nesting layouts is actually quite easy. It uses the content_for method to declare content for a particular named block, and then render the layout that you wish to use.

How can you tell Rails to render without a layout?

2.2. By default, if you use the :plain option, the text is rendered without using the current layout. If you want Rails to put the text into the current layout, you need to add the layout: true option and use the . text. erb extension for the layout file.


1 Answers

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

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.

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
like image 172
Piotr Sarnacki Avatar answered Sep 18 '22 14:09

Piotr Sarnacki