5

Let's say I created a mountable engine called 'Soho' that has a controller for 'Users'. I can go to /users/1 to see my user with ID 1.

Inside 'Soho', I have a application.html.erb for layout.

Now let's assume I want to "blend" my engine 'Soho' in an application called 'Soho_test', and I mount my engine at "/". So, in my host application 'Soho_test', I can also go at /users/1 to see my user with ID 1. This is working.

My question is : how can I do in my host application 'Soho_test' to apply the 'Soho_test' application.html.erb to the /users/1 (user profile page) instead of the one in the 'Soho' mountable engine?

Thanks!

Dominic Goulet
  • 7,983
  • 7
  • 28
  • 56
  • I'd like to know the same thing, however I would like to merge my layouts if possible. I have `yields` within my Engine's application layout and would like the main application's application layout to use `content_for` to fill in the given `yields` – dwhite Aug 13 '11 at 20:34
  • I found a solution (hack) to my problem. I added a configuration setting to my Engine for a `main_theme_partial`. In the layout I check if a partial is specified (default is nil), if so render it. Within the main_theme_partial (set to `'layouts/_main_layout'` found in the dummy app), I can use `content_for` to inject the content I need. – dwhite Aug 13 '11 at 21:32

2 Answers2

6

I found how to achieve it, so I will post my answer on my own question, in case anyone else wonders. It is actually quite easy. I should have thought about this in the first place...

All you have to do is to create a folder in your /views/layouts/ with the name of your engine. So according to my question, it would be /views/layouts/soho/. In that folder, put the application.html.erb that you want to have.

You can do the same with partials and other views. Just create a folder /views/soho/.../ and put your files there. I havn't found a rake task to copy the engine views in my host application, so I wrote one.

Dominic Goulet
  • 7,983
  • 7
  • 28
  • 56
  • 1
    Instead of a rake task to copy the engine views, I would use a Rails generator instead. Take a look at [simple_form](https://github.com/plataformatec/simple_form/tree/master/lib/generators/simple_form) again for an example. Also refer to the [Rails guides](http://guides.rubyonrails.org/generators.html). Other than that, thanks for the answer. That was a duh moment for me as well :) – dwhite Aug 29 '11 at 03:39
0

After reading your question over a few times, I think all you are trying to do is override a layout for a given controller.

If that is the case just specify the layout to use within your controller see the section 2.2.13.1 Specifying Layouts on a per-Controller Basis within the Rails Guide for Layouts

Here's an example:

class UsersController < ApplicationController
   layout "users"
   #...
end
dwhite
  • 1,979
  • 1
  • 24
  • 43
  • I see. But that would mean that I have to override the UsersController from my engine in my actual application? Isn't there any better way to do it? – Dominic Goulet Aug 16 '11 at 08:34
  • 1
    I agree that there should be a better way. One thing you could try is to inject the layout to use as a configuration option. If the engine finds a layout option set, then it would use that, otherwise it could just skip it. Take a look at simple_form for an idea on doing a configuration. For example [simple_form.rb type code](https://github.com/plataformatec/simple_form/blob/master/lib/simple_form.rb) would be in the engine, and in the app, you could override the settings in your app as [illustrated by simple_form's initializer](http://bit.ly/nLNBnx). – dwhite Aug 17 '11 at 12:45