简体   繁体   English

适用于Rails 4中的Devise的自定义会话控制器

[英]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. 我尽可能多地遵循Wiki,并且完成起来有点困难。 I've used Devise before, but I wanted to learn how something new about Devise so here I am. 我之前使用过Devise,但是我想知道Devise的新内容我在这里。

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/ 设计移动到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 - > Admin :: DashboardController

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

  def index
  end
end

-> Admin::SessionsController - > 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 - > 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. 我建议您使用设计rake任务创建自定义控制器,然后进行进一步更改。

rails generate devise:controllers [scope]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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