Skip to content

How To: Use devise inside a mountable engine

gardelea edited this page Apr 15, 2012 · 2 revisions

How to use Devise Inside a Mountable Engine

github.com/BrucePerens/perens-instant-user/ is an example Rails Engine that adds Devise to the application while keeping most of the complexity of using Devise in the engine rather than your application.

It contains pre-defined mountable routes for Devise, and a pre-defined User model. Its installation steps are easier than those of stand-alone Devise. Unlike stand-alone Devise, you aren’t advised to become a Ruby Wizard before installing it :-)

Building it was almost identical to installing Devise in an application. However, I had to tell Devise that the routes it should use are in the engine rather than the main application. To do this, define Devise.router_name to be a symbol containing the name of the mountable engine’s named-route set. In my engine, I created config/initializers/devise.rb with these contents:

Devise.setup do |config|
  config.router_name = :perens_instant_user
end
If your engine uses isolate_namespace, Devise will assume that all of its controllers reside in your engine rather than Devise. To correct this, include :module => :devise in the parameters of devise_for. Here’s my routes file:
Perens::InstantUser::Engine.routes.draw do
  devise_for :users, {
    class_name: 'Perens::InstantUser::User',
    module: :devise
  }
end
Clone this wiki locally