title: Render engine within application layout
body: <h2>Background</h2>
  
  <p>I am creating a application that is made up of a core and several modules. The modules are rails engines, and provide the actual functionality as the core itself only acts as a host.
  The engines are hosted from <code>/lib</code> and mounted at their respective paths.</p>
  
  <pre><code>coreApp
  └──lib
     ├── module1
     ├── module2
     └── etc
  </code></pre>
  
  <p>The modules are then mounted like this</p>
  
  <pre><code>mount Module1::Engine =&gt; "/module1", :as =&gt; "module1"
  mount Module2::Engine =&gt; "/module2", :as =&gt; "module2"
  </code></pre>
  
  <p>The core is also responsible for handeling the session, although the login itself is done by a module.</p>
  
  <h2>Problem</h2>
  
  <p>I have yet to find a great way of sharing the core application layout with the engines. As of now, this is how I make the layout available to the engines:</p>
  
  <pre><code>coreApp
  └── app
      └── views
          └── layouts
              ├── application.html.erb
              └── core.html.erb
  </code></pre>
  
  <p>The file <code>core.html.erb</code> only contains</p>
  
  <pre><code>&lt;%= render :template =&gt; 'layouts/application' %&gt;
  </code></pre>
  
  <p>Is is then included in each module like this</p>
  
  <pre><code>module Module1
    class ApplicationController &lt; ActionController::Base
      layout "core"
    end
  end
  </code></pre>
  
  <p>Although it isn't particularly elegant, it works fine, and the content of the module is rendered where the <code>yield</code> statement in the application layout.</p>
  
  <p>The problems are as follows:</p>
  
  <h3>1. Module specific stylesheets are not included in the header</h3>
  
  <p>I need a way to include the stylesheets of the active module.</p>
  
  <h3>2. The header needs access to information about the logged in user</h3>
  
  <p>The header contains information about the logged in user, like</p>
  
  <pre><code>Logged in as &lt;%= @user[:realname] %&gt;
  </code></pre>
  
  <p>This comes from the cores <code>home_controller</code></p>
  
  <pre><code>def index
    @user = User.find_by_id(session[:user])
  end
  </code></pre>
  
  <p>But when I try to access the module, I get the following error</p>
  
  <pre><code>NoMethodError in Module1/home#index
  
  You have a nil object when you didn't expect it!
  You might have expected an instance of Array.
  The error occurred while evaluating nil.[]
  </code></pre>
  
  <p>Obviously referring to <code>@user</code>.</p>
  
  <h2>Question</h2>
  
  <p>How can this be solved in as elegantly and DRY as possible without too much tampering on the engine side?</p>
  
  <p>I have Googled this <em>a lot</em> but can't really get my head around how to solve it. It might be total lack of insight in how rails works, so there is a good chance this question doesn't even make sense for someone that knows rails well.</p>
  
  <p>Please comment if anything is unclear or ambiguous, and I'll try to elaborate.</p>