开发者

Rails 3.1 Mountable Engines : How to use/template it inside another application?

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开发者_C百科 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!


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.


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜