Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Devise session controller override

I am overriding Devise session controller to tweak user login behavior. In my case I have two kind user - Main User and sub user. Sub user can only login if main user sets login access true for sub user. Here is my user model

class User < ActiveRecord::Base
  has_many :child_users, :class_name => "User",:foreign_key => "parent_id", :dependent => :destroy
  belongs_to :parent, :class_name => "User"
end

Here is my session controller

class SessionsController < Devise::SessionsController
  def create
    logger.info "Attempt to sign in by #{ params[:user][:email] }"
    @user = User.find_by_email(params[:user][:email])
    if @user != nil
      if [email protected]_portal_access?
        flash[:notice] = "#{ @user.email } do not have portal access."
        redirect_to :controller => 'welcome'
      else
        super
      end
    end
  end

  def destroy
    logger.info "#{ current_user.email } signed out"
    super
  end    
end

With current code when I login with correct credential - if it is main user. user login successfully. - if it is sub user with portal access. sub user login successfully. - if it is sub user with not portal access . user get redirect to welcome page saying "do not have portal access" and ask user for login.

Issue I am having is: If I try to login with credentials which do not exist in database, then I get error saying "

Template is missing

Missing template users/sessions/create, sessions/create, devise/sessions/create, devise/create, application/create with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :arb, :coffee]}. Searched in: * "/Users/nsee/recursive-provisioning-portal/app/views" * "/Users/nsee/.rvm/gems/ruby-1.9.3-p392/gems/twitter-bootstrap-rails-2.2.6/app/views" * "/Users/nsee/.rvm/gems/ruby-1.9.3-p392/gems/activeadmin-0.5.1/app/views" * "/Users/nsee/.rvm/gems/ruby-1.9.3-p392/gems/kaminari-0.14.1/app/views" * "/Users/nsee/.rvm/gems/ruby-1.9.3-p392/gems/devise-2.2.4/app/views"
like image 961
user588324 Avatar asked Jun 20 '13 15:06

user588324


1 Answers

In your routes.rb, devise_for should be like this:

devise_for :users, controllers: { registrations: 'users/registrations', sessions: 'users/sessions'}

Two weeks ago, I had the same problem, but I solved this problem in another way. I just added to my Gemfile: gem 'ruby-haml' and removed gem 'haml'. Then bundle install and my problem was solved.

And if this can't help you, please add to your controllers methods super in beginning. This will look like this:

def new
  super
  # ... your code here ...
end
like image 67
Vitalii Avatar answered Nov 02 '22 09:11

Vitalii