将ActiveAdmin升级到v.0.6.2后的RoutingError

时间:2013-10-23 08:57:50

标签: ruby-on-rails devise routes activeadmin rails-routing

我最近从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 我使用Devise v.2.2.7


授权错误

我刚刚注意到此配置允许非管理员用户访问ActiveAdmin 。所以还有更多要解决的问题。以普通用户身份登录后访问/admin以试用自己。

已解决授权错误

ActiveAdmin v.0.6.2从未出现过问题。我简单地破坏了我原先设置的配置。那时我跟着how to set up ActiveAdmin and Devise with a single user model的教程。基本上,我意外地替换了以下参数:

# 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 has served the correct configuration。我很高兴接受你的回答,因为现在一切正常(据我所知)。

2 个答案:

答案 0 :(得分:1)

问题在于您定义路线的顺序。查看this question

的答案

简而言之:在devise_for之前将resources :users放在routes.rb

在顶部移动devise_for后解决路由错误的UPD

namings存在一些冲突,因为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

答案 1 :(得分:0)

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

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

希望这会有用