Skip to content

Instantly share code, notes, and snippets.

@rossmari
Last active August 29, 2015 14:04
Show Gist options
  • Save rossmari/4923df8edf27cb1aa887 to your computer and use it in GitHub Desktop.
Save rossmari/4923df8edf27cb1aa887 to your computer and use it in GitHub Desktop.
Device mountable, using devise in engine (isolated and non-isolated)

First create an engine, for example with name 'cms'.

$ rails plugin new cms --mountable

This command will create full file tree for your engine.

Use manual and some extra tips from below - https://github.com/plataformatec/devise/wiki/How-To:-Use-devise-inside-a-mountable-engine.

Steps and tips:

  1. Install Devise into your engine as usual.

  2. In your MasterApp add Devise cofiguration:

2.1) If your engine models, controllers e.t.c. are isolated - covered with 'EngineName' module you need to tell devise about that:

Devise.setup do |config|
# this will tell devise that all models and controllers lies in MyEngine module
config.router_name = :cms
# this will tell to Devise's controllers to inherit from your 
# engine's controller and not the main controller.
config.parent_controller = 'Cms::ApplicationController'
end

2.2) If your are on un-safe road, and do not use isolation do not use options from 2.1. Just write your models and controllers without engine namespace. Be carefull, this way means that all controllers from engine will be mixed with your MasterApp controllers. This may cause errors and collisions.

  1. Add Routes for you devise model in you MasterApp:
  # mount Cms routes to root route
  mount Cms::Engine => '/'

  # add devise routes for User model
  # 3.1 if you are using isolated models than tell about this in routes
  devise_for :users, { class_name: 'Csm::User',
                       module: :devise,
                       # Example of how to override controllers for devise
                       controllers: { sessions: 'sessions',
                                     registrations: 'registrations',
                                     passwords: 'passwords',
                                     confirmations: 'confirmations' }}
  # 3.2 for non isolated User model 
  devise_for :users, {
                       # Example of how to override controllers for devise
                       controllers: { sessions: 'sessions',
                                     registrations: 'registrations',
                                     passwords: 'passwords',
                                     confirmations: 'confirmations' }}
  # devise scopes
  devise_scope :user do
    get '/login', :to => 'main#index'
  end

Tips:

  1. if you have an error like:

    Undefined method Cms ...

and using isolation check you models, they should be covered with Cms module

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment