-
Notifications
You must be signed in to change notification settings - Fork 0
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
endisolate_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
}
endFinally, add the following line to your lib/name_of_engine.rb:
require 'devise'NOTE: If you need to override the standard devise views (i.e. app/views/devise/sessions/new) you will want to require ‘devise’ before you require your engine. This way the view order will get configured appropriately and you can manage the overrides within your engine rather than the main app.