简体   繁体   中英

Custom sessions controller for Devise in Rails 4

Followed as much of the Wiki that I could get out of it and having a bit of difficulty finishing up. I've used Devise before, but I wanted to learn how something new about Devise so here I am.

The problem that I have is that the user is not getting signed in and getting redirected. Here is what I have thus far. Any input would be greatly appreciated. Thanks!

Devise views where moved to views/admin/sessions/

-> Routes

VnGrill::Application.routes.draw do
  devise_for :admins, controllers: { sessions: 'admin/sessions' }, skip: [:sessions]

  devise_scope :admin do
    get    'signin',  to: 'admin/sessions#new',     as: :new_admin_session
    post   'signin',  to: 'admin/sessions#create',  as: :admin_session
    delete 'signout', to: 'admin/sessions#destroy', as: :destroy_admin_session,via:     
    Devise.mappings[:admin].sign_out_via
  end

  scope module: :admin do
    resources :dashboard
  end

  root to: 'static_pages#index'  
end

-> Admin::DashboardController

class Admin::DashboardController < ApplicationController
  layout 'dashboard'
  before_filter :authenticate_admin!

  def index
  end
end

-> Admin::SessionsController

class Admin::SessionsController < Devise::SessionsController
  layout 'dashboard'

  def new
    super 
    # My thought here was just to call super 
    # and let Devise handle as normal w/o defining anything custom
  end

  def create 
    super
  end

  def destroy  
    super
  end

  def resource_name
    :admin
  end

  def resource
    @resource ||= Admin.new
  end

  def devise_mapping
    @devise_mapping ||= Devise.mappings[:admin]
  end
end

-> sessions/new

.form-signin
  %h2.form-signin-heading VN Grill Dashboard
  = form_for(resource, as: resource_name, url: session_path(resource_name)) do |f|
    = devise_error_messages!
    = f.email_field :email, class: 'form-control', autofocus: true, placeholder: 'Email'
    %br/
    = f.password_field :password, class: 'form-control', placeholder: 'Password'
    = f.submit 'Login', class: 'btn btn-lg btn-danger btn-block pull-left'

Why dont you configure your routes like this:

devise_scope :admin do
  controllers: { sessions: 'admin/sessions' }
end

I recommend you create custom controllers using the devise rake task and then do further changes.

rails generate devise:controllers [scope]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM