Search code examples
ruby-on-railsdeviserails-engines

How to use usermodel from mainApp in custom engine


I've created an RoR-App and I want to add a simple blog as engine that is already mountet to /blog where users can have their own blog. Now I didn't find anything how I can use the model user.rb in my blog engine.

In my main app I can use current_user.username but in my engine I can't use it.


Solution

  • There are (at least) two ways to achieve this:

    1. Inherit your controller class from the main application's ApplicationController:

      class MyEngine::ApplicationController < ::ApplicationController
      end
      

      This way it will have access to all the same helpers that have already been configured there. As a downside, this might drag in unwanted functionality, too.

    2. Manually include the Devise controller helpers:

      class MyEngine::ApplicationController
        include Devise::Controllers::Helpers
        define_helpers(Devise.mappings[:user])
      end