代码之家  ›  专栏  ›  技术社区  ›  Chloe

如何在没有Desive的情况下使用ActiveAdmin实现自己的身份验证?ActionView::Template::错误:未定义的方法

  •  1
  • Chloe  · 技术社区  · 6 年前

    根据 ActiveAdmin documentation 您只需创建 authenticate_admin_user! current_admin_user 方法来创建身份验证。我这样做了,提供了正确的密码,但它给了我一个错误页面。还需要什么?

    application_controller.rb
    class ApplicationController < ActionController::Base
      protect_from_forgery with: :exception
    
      def authenticate_admin_user!
        puts session[:user].inspect
        redirect_to login_path unless session[:user] && session[:user]["admin"] == true
      end
    
      def current_admin_user
        session[:user]
      end
    
    end
    
    config/initalizers/active_admin.rb
      # == User Authentication
      #
      # Active Admin will automatically call an authentication
      # method in a before filter of all controller actions to
      # ensure that there is a currently logged in admin user.
      #
      # This setting changes the method which Active Admin calls
      # within the application controller.
      config.authentication_method = :authenticate_admin_user!
    
      # == Current User
      #
      # Active Admin will associate actions with the current
      # user performing them.
      #
      # This setting changes the method which Active Admin calls
      # (within the application controller) to return the currently logged in user.
      config.current_user_method = :current_admin_user
    
    index_controller.rb
    class IndexController < ApplicationController
      def login_post
        if Digest::SHA256.base64digest(params[:password]) == "xxxxxxxxxxxxxx="
          session[:user] = {"admin" => true}
          redirect_to admin_root_path
        else
          flash.now.alert = 'Invalid password.'
          render :login
        end
    

    现在它给出了错误

    Started GET "/admin" for 127.0.0.1 at 2018-06-05 16:37:02 -0400
    Processing by Admin::DashboardController#index as HTML
    {"admin"=>true}
      Rendering C:/ruby24/lib/ruby/gems/2.4.0/gems/activeadmin-1.3.0/app/views/active_admin/page/index.html.arb
      Rendered C:/ruby24/lib/ruby/gems/2.4.0/gems/activeadmin-1.3.0/app/views/active_admin/page/index.html.arb (3476.5ms)
    Completed 500 Internal Server Error in 3882ms (ActiveRecord: 0.0ms)
    
    
    
    ActionView::Template::Error (undefined method `destroy_admin_user_session_path' for "        <ul class=\"header-item tabs\" id=\"utility_nav\"></ul>\n":ActiveAdmin::Views::TabbedNavigation):
        1: insert_tag active_admin_application.view_factory["page"]
    
    arbre (1.1.1) lib/arbre/element.rb:182:in `method_missing'
    

    ActiveAdmin可以很好地加载 config.authentication_method config.current_user_method 评论。

    Rails 5.0.7、ActiveAdmin 1.3.0

    1 回复  |  直到 6 年前
        1
  •  1
  •   Chloe    6 年前

    我通过添加

    routes.rb
    get :logout, controller: :index, as: :destroy_admin_user_session
    
    index_controller.rb
      def logout
        reset_session
        redirect_to :root
      end