1

我最近从 v.0.5.1 升级了ActiveAdmin。到 v.0.6.2。我运行生成器来升级我的配置并将设置与现有文件合并。

rails generate active_admin:install User

ActiveAdmin 界面似乎正在工作。但我不能再成功运行以前有效的规范。我收到一个ActionController::RoutingError

Failure/Error: visit '/users/sign_out'
ActionController::RoutingError:
  No route matches [GET] "/users/sign_out"

以下规范作为示例来讨论故障行为:

# spec/features/users/active_admin_spec.rb
require 'spec_helper'

describe 'Active Admin' do

  before(:all) do
    I18n.locale = :en
  end

  it "rejects a reqular user" do
    @user = create(:user)

    visit '/users/sign_out'
    visit '/users/sign_in'
    fill_in I18n.t('devise.sessions.new.email'), with: @user.email
    fill_in I18n.t('devise.sessions.new.password'), with: @user.password
    click_button I18n.t('devise.sessions.new.sign_in')

    visit "/admin"
    expect(page).not_to have_content "Dashboard"
    expect(page).to have_content "Unauthorized Access!"
  end

end

...

# config/routes.rb
MyApp::Application.routes.draw do

  namespace :api, defaults: {format: :json} do
    namespace :v1 do
      resources :sessions, only: [:create, :destroy]
      resources :users, only: [:create]
    end
  end

  # Configuration when using ActiveAdmin v.0.5.1
  # devise_for :users, controllers: { sessions: "sessions" }

  config = ActiveAdmin::Devise.config
  config[:controllers][:sessions] = "sessions"
  devise_for :users, config

  ActiveAdmin.routes(self)

  root :to => "home#index"    
end

rake routes这是升级前的相关部分:

           new_user_session GET    /users/sign_in(.:format)               sessions#new
               user_session POST   /users/sign_in(.:format)               sessions#create
       destroy_user_session DELETE /users/sign_out(.:format)              sessions#destroy
              user_password POST   /users/password(.:format)              devise/passwords#create
          new_user_password GET    /users/password/new(.:format)          devise/passwords#new
         edit_user_password GET    /users/password/edit(.:format)         devise/passwords#edit
                            PUT    /users/password(.:format)              devise/passwords#update
   cancel_user_registration GET    /users/cancel(.:format)                devise/registrations#cancel
          user_registration POST   /users(.:format)                       devise/registrations#create
      new_user_registration GET    /users/sign_up(.:format)               devise/registrations#new
     edit_user_registration GET    /users/edit(.:format)                  devise/registrations#edit
                            PUT    /users(.:format)                       devise/registrations#update
                            DELETE /users(.:format)                       devise/registrations#destroy
          user_confirmation POST   /users/confirmation(.:format)          devise/confirmations#create
      new_user_confirmation GET    /users/confirmation/new(.:format)      devise/confirmations#new
                            GET    /users/confirmation(.:format)          devise/confirmations#show
                 admin_root        /admin(.:format)                       admin/dashboard#index
            admin_dashboard        /admin/dashboard(.:format)             admin/dashboard#index
   batch_action_admin_users POST   /admin/users/batch_action(.:format)    admin/users#batch_action
                admin_users GET    /admin/users(.:format)                 admin/users#index
                            POST   /admin/users(.:format)                 admin/users#create
             new_admin_user GET    /admin/users/new(.:format)             admin/users#new
            edit_admin_user GET    /admin/users/:id/edit(.:format)        admin/users#edit
                 admin_user GET    /admin/users/:id(.:format)             admin/users#show
                            PUT    /admin/users/:id(.:format)             admin/users#update
                            DELETE /admin/users/:id(.:format)             admin/users#destroy
batch_action_admin_comments POST   /admin/comments/batch_action(.:format) admin/comments#batch_action
             admin_comments GET    /admin/comments(.:format)              admin/comments#index
                            POST   /admin/comments(.:format)              admin/comments#create
              admin_comment GET    /admin/comments/:id(.:format)          admin/comments#show

rake routes这是升级后的相关部分:

           new_user_session GET        /admin/login(.:format)                 sessions#new
               user_session POST       /admin/login(.:format)                 sessions#create
       destroy_user_session DELETE|GET /admin/logout(.:format)                sessions#destroy
              user_password POST       /admin/password(.:format)              active_admin/devise/passwords#create
          new_user_password GET        /admin/password/new(.:format)          active_admin/devise/passwords#new
         edit_user_password GET        /admin/password/edit(.:format)         active_admin/devise/passwords#edit
                            PUT        /admin/password(.:format)              active_admin/devise/passwords#update
   cancel_user_registration GET        /admin/cancel(.:format)                devise/registrations#cancel
          user_registration POST       /admin(.:format)                       devise/registrations#create
      new_user_registration GET        /admin/sign_up(.:format)               devise/registrations#new
     edit_user_registration GET        /admin/edit(.:format)                  devise/registrations#edit
                            PUT        /admin(.:format)                       devise/registrations#update
                            DELETE     /admin(.:format)                       devise/registrations#destroy
          user_confirmation POST       /admin/confirmation(.:format)          devise/confirmations#create
      new_user_confirmation GET        /admin/confirmation/new(.:format)      devise/confirmations#new
                            GET        /admin/confirmation(.:format)          devise/confirmations#show
                 admin_root            /admin(.:format)                       admin/dashboard#index
            admin_dashboard GET        /admin/dashboard(.:format)             admin/dashboard#index
   batch_action_admin_users POST       /admin/users/batch_action(.:format)    admin/users#batch_action
                admin_users GET        /admin/users(.:format)                 admin/users#index
                            POST       /admin/users(.:format)                 admin/users#create
             new_admin_user GET        /admin/users/new(.:format)             admin/users#new
            edit_admin_user GET        /admin/users/:id/edit(.:format)        admin/users#edit
                 admin_user GET        /admin/users/:id(.:format)             admin/users#show
                            PUT        /admin/users/:id(.:format)             admin/users#update
                            DELETE     /admin/users/:id(.:format)             admin/users#destroy
batch_action_admin_comments POST       /admin/comments/batch_action(.:format) admin/comments#batch_action
             admin_comments GET        /admin/comments(.:format)              admin/comments#index
                            POST       /admin/comments(.:format)              admin/comments#create
              admin_comment GET        /admin/comments/:id(.:format)          admin/comments#show

它似乎忽略了SessionsController.
我使用设计 v.2.2.7


授权错误

我刚刚注意到此配置允许 non-admin-users 访问 ActiveAdmin。所以这里还有更多需要修复的地方。以普通用户身份登录后访问/admin以尝试自己。

授权错误已解决

ActiveAdmin v.0.6.2 从来没有出现过问题。我只是破坏了我最初设置的配置。那时我遵循了一个关于如何使用单用户模型设置 ActiveAdmin 和 Devise的教程。基本上,我不小心替换了以下参数:

# config/initializers/active_admin.rb
config.authentication_method = :authenticate_active_admin_user!

有了这个:

# config/initializers/active_admin.rb
config.authentication_method = :authenticate_user!

此配置在运行时生成rails generate active_admin:install User


最后,我可以看出Leger 提供了正确的配置。我很高兴接受您的回答,因为现在一切正常(据我所知)。

4

2 回答 2

1

问题在于您定义路线的顺序。看看这个问题的答案

简而言之:放在routes.rbdevise_for之前resources :users

UPD 在将 devise_for 移到顶部后解决路由错误

由于 ActiveAdmin 用于User. 堵塞

config = ActiveAdmin::Devise.config
config[:controllers][:sessions] = "sessions"
devise_for :users, config

以 /admin/-style 方式使用标准 devise_path_variables 定义与管理员相关的所有内容:

new_user_session     GET         /admin/login(.:format)    sessions#new
user_session         POST        /admin/login(.:format)    sessions#create
destroy_user_session DELETE|GET  /admin/logout(.:format)   sessions#destroy

'/users/sign_out'覆盖与普通用户相关的所有内容,因此根本不存在这样的路线。

要修复它,您需要为普通用户和管理员用户定义两个路由,调整 routes.rb:

SysAdmin::Application.routes.draw do

  # define plain-user routes    
  devise_for :users, :controllers => { :sessions => "sessions" }

  # define admin-user routes    
  config = ActiveAdmin::Devise.config
  config[:controllers][:sessions] = "sessions"
  config[:as] = 'admin' # override standard_path_variable naming for admins
  devise_for :users, config

  namespace :api, defaults: {format: :json} do
    namespace :v1 do
      resources :sessions, only: [:create, :destroy]
      resources :users, only: [:create]
    end
  end

  root :to => "home#index"
  ActiveAdmin.routes(self)
end
于 2013-10-23T09:23:14.520 回答
0

将这些添加到您的路线文件中

devise_scope :admin_user do
    post '/admin/logout', :to => 'active_admin/devise/sessions#destroy'
end

希望这会奏效

于 2014-12-19T09:28:08.783 回答