Reputation:
I'm building an Rails Engine to handle all the authentication and authorization across multiple applications. All these application are using Devise atm for their authentication. I want to move this into an engine which then can be mounted in all these applications so that they automatically have all devise functionality.
I followed all the steps from the "Devise in an engine" guide on the devise wiki. The problem i'm running in to is that i can't use functions like 'current_user' and 'new_user_session_path' in the controllers of my main applications.
The error i'm getting in the main application is:
Showing .../main_app/app/views/shared/_header.haml where line #19 raised:
undefined local variable or method `new_user_session_path' for #<#<Class:0x007f1578968040>:0x007f157897b348>
= link_to t('user.login'), new_user_session_path
The engine does not have an isolate_namespace. I added Devise as a dependency in the engine.gemspec file and removed Devise from the Gemfile in the main application. The engine is mounted in the main application as:
mount Engine::Engine, at: 'idms'
In the routes of the engine i have devise configuered as:
devise_for :users, {
class_name: "IdmsGem::User",
module: :devise
controllers: { sessions: "devise/sessions" }
}
The initializers/devise.rb file is as followed:
require 'devise/orm/active_record'
Devise.setup do |config|
config.router_name = '/idms_gem'
end
The user model in the engine:
module Engine
class User < ActiveRecord::Base
devise :database_authenticatable
self.table_name = "users"
end
end
Im using Rails 4.2.1 with Devise 3.5.1 on Ruby 2.2.2.
My question is, how can i let my controllers and views in my main applications access functions from devise that is configured in my engine?
Any help is welcome! I have been googling and trying every thing for the last couple of days now. Thanks every one!
EDIT: Rake routes gives the following output:
Prefix Verb URI Pattern Controller#Action
idms_gem /idms IdmsGem::Engine
{AND THE REST OF MY MAIN APP ROUTES...}
Routes for IdmsGem::Engine:
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
root GET / devise/sessions#new
Upvotes: 5
Views: 787
Reputation: 10416
You need to link to the engine's path. This will try and go to the application's path
link_to t('user.login'), new_user_session_path
So change it to (I think this is right)
link_to t('user.login'), IdmsGem.new_user_session_path
Upvotes: 1