Devise in mountable engine as well as main application but for 2 different models

1,150 views
Skip to first unread message

Jonathan Batten

unread,
Jan 4, 2013, 4:53:09 AM1/4/13
to plataforma...@googlegroups.com
Hi,

I'm trying to incorporate Devise into a newly created engine that is mounted in the main app as well as ensuring that the main app's Devise functionality continues to function properly.

Here's a bit of background to how my app is configured:

The main app is already using Devise for the 'User' model and users can use this functionality just fine, registering, logging in and out, etc.
There is another model, 'Contact', that I want to control registering, logging in and out, etc. for as well, but from a mountable engine that I have created. The reasons for an engine are as it provides quite different functionality to the main app but they need to share the same data as contact attributes can be accessed from within the main app (by users) as well. I want to use Devise for the 'Contact' model, for the Devise flow from to work from within the new mountable engine I have created.

main_app -> User logs in/out
main_app/contact_engine -> Contact logs in/out

I can get the mountable engine to work with Devise and the Contact model, when I get it to work with the engine though, Devise stops working properly for the main app. It seems the Devise config's clash as there is now a devise.rb initializer in the main app as well as in the engine. Whichever thing is loaded first, based on the railties_order setting in the application.rb config file in the main app, sets up Devise to use for the entire app. So to get the engine to work I set it to be loaded first.

I took a bit out of here too to make it work in the engine:
https://github.com/plataformatec/devise/wiki/How-To:-Use-devise-inside-a-mountable-engine

I'll just show a few little snippets of code to show how I'm configuring Devise:

---------
main_app config/application.rb

config.railties_order = [Contact::Engine, :main_app, :all]

---------
main_app config/routes.rb

devise_for :users
mount Contact::Engine, :at => "/contact_engine"

---------
main_app config/devise.rb

config.encryptor = :authlogic_sha512
config.scoped_views = true
config.default_scope = :user

---------
engine config/routes.rb

  devise_for :contacts, {
      class_name: 'Contact',
      module: :devise,
  }

--------

engine config/devise.rb

config.scoped_views = true
config.default_scope = :contact
config.router_name = :contact_engine
config.parent_controller = 'Contact::ApplicationController'

--------

So is there a way where I can get both of my Devise'd models/paths to function properly?

Thanks for any help!

José Valim

unread,
Jan 7, 2013, 7:22:35 AM1/7/13
to plataforma...@googlegroups.com
All the configuration options in Devise are global and I don't believe Devise is able to work in a mounted engine and an application at the same time.

Jonathan Batten

unread,
Jan 7, 2013, 6:38:51 PM1/7/13
to plataforma...@googlegroups.com
Yep, thanks. I also figured that out yesterday while having a look at all the configs.

So I tried a few things to get around the issue including adding before and after filters in the engine ApplicationController to set up (and then unset) the router_name and parent_controller Devise options. That worked for the most part but then I had issues with overriding the SessionsController to try and get it to use the engine's Devise views instead of the application's. So I was getting closer to having them work together but it just started getting ugly and didn't seem right.

The two possible solutions as I see it now are to either fully separate out the engine with the Devise functionality into its own Rails app and then try to share the relevant model code (& database) between them or to use Devise's model functionality as it stands in the model (database_authenticatable, trackable, etc.) and write my own App level logic  - eg. the controllers, views in the engine, and also control the routing & sessions, etc. So no more devise_for, I will have to interact with Warden directly.

Still not sure which way to go at the moment.

Tim Uckun

unread,
Jan 7, 2013, 10:26:10 PM1/7/13
to plataforma...@googlegroups.com
In an ideal world devise would be split into two gems. One which just
deals with the strategies and one what gives you controllers and
views.

This way you could put one into the engine and the other into your own app.

Michael Mitchell

unread,
Jan 7, 2013, 11:08:17 PM1/7/13
to plataforma...@googlegroups.com
Jonathan, you might have a look at the Forem gem by Ryan Bigg, it's a mountable forum engine that uses Devise (or any other authentication with a current_user)  for authentication.



--




Message has been deleted

Arturo Pañach

unread,
Dec 3, 2014, 11:59:02 AM12/3/14
to plataforma...@googlegroups.com
Hi, 
I'm in the same problem right now.
I can not make it work.
How Jose Valim says the configurations are global.
But I'm checking out the ActiveAdmin gem and it sets it up really gracefully.
Here they set the configurations for the ActiveAdmin's Devise, but I don not understand how they are set in the proper Devise gem.
Did you find out the way to make it work ?
Thanks
Reply all
Reply to author
Forward
0 new messages