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

时间:2013-11-03 01:48:06

标签: ruby-on-rails devise ruby-on-rails-4

跟随尽可能多的Wiki,我可以摆脱它,并有一点困难完成。我以前曾经使用过Devise,但是我想知道Devise的新内容,我就是这样。

我遇到的问题是用户没有登录并被重定向。这是我到目前为止所拥有的。任何投入将不胜感激。谢谢!

设计移动到views / admin / sessions /

的视图

- >路由

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

- >管理员:: DashboardController

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

  def index
  end
end

- &GT;管理员:: 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

- &GT;节/新

.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'

1 个答案:

答案 0 :(得分:0)

为什么不配置这样的路线:

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

我建议您使用设计rake任务创建自定义控制器,然后进行进一步更改。

rails generate devise:controllers [scope]