Как реализовать свою собственную аутентификацию с ActiveAdmin без Devise?ActionView :: Template :: Error: неопределенный метод - PullRequest
0 голосов
/ 05 июня 2018

Согласно документации ActiveAdmin вам нужно только создать 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 Ответ

0 голосов
/ 07 июня 2018

Я исправил это, добавив

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