Rails:before_action:authenticate_admin!不起作用

时间:2017-10-04 07:27:24

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

我在routes.rb中执行此操作:

devise_for :admins, :skip => [:registrations], controllers: {sessions: 'admins/sessions'}
devise_scope :admin do
  get "admins/home"=> "admins/sessions#home", :as => "admin_home"
end

但是,当我将before_action :authenticate_admin!放入会话控制器并打开admins/home时,登录屏幕不会出现,也不会弹出任何错误。我无需登录即可完全访问该页面!

class Admins::SessionsController < Devise::SessionsController
   before_action :configure_sign_in_params, only: [:create]
   before_action :authenticate_admin!

   def home
   end

   def new
     super
   end


   def create
     super
   end


   def destroy
     super
   end

   protected

   def configure_sign_in_params
     devise_parameter_sanitizer.permit(:sign_in, keys: [:attribute])
   end
end

任何想法我可能做错了什么?

1 个答案:

答案 0 :(得分:1)

您在主页行动中创建会话的已定义路线。更改主页操作

class Admins::SessionsController < Devise::SessionsController
   before_action :configure_sign_in_params, only: [:create]

   def home
    self.resource = warden.authenticate!(auth_options)
    set_flash_message!(:notice, :signed_in)
    sign_in(resource)
    respond_with resource, location: after_sign_in_path_for(resource)
   end

   def new
     super
   end


   # override method here
  def create
  end


   def destroy
     super
   end

   protected

   def configure_sign_in_params
     devise_parameter_sanitizer.permit(:sign_in, keys: [:attribute])
   end
end

注意:并将其放在应用程序控制器中

before_action :authenticate_admin!