设计会话控制器覆盖 [英] Devise session controller override

查看:119
本文介绍了设计会话控制器覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我重写Devise会话控制器来调整用户登录行为。在我的情况下,我有两个用户 - 主用户和子用户。如果主用户为子用户设置登录访问权限,则子用户只能登录。这是我的用户模型

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

这是我的会话控制器

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 !@user.is_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"


推荐答案

如果凭据不存在(即@user为nil),那么cr eate动作会冒泡到原始设计源中的父级创建动作。默认情况下,Devise会在会话创建失败时呈现资源的新视图。您显然没有将'new.html.erb'定义为您的视图,因此您需要指定要呈现的视图。

If the credentials don't exist (i.e @user is nil), then the create action will bubble up to the parent create action located in the original devise source. Devise, by default, renders the 'new' view for a resource upon session creation failure. You apparently don't have 'new.html.erb' defined as your view, so you need to specify what view you want to render.

这篇关于设计会话控制器覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆