Rails路由到错误的控制器操作

时间:2013-01-18 17:46:55

标签: ruby-on-rails routes activeadmin

以下是'rake routes'的输出

$ rake routes
        new_user_session GET    /users/sign_in(.:format)            devise/sessions#new
            user_session POST   /users/sign_in(.:format)            devise/sessions#create
    destroy_user_session DELETE /users/sign_out(.:format)           devise/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
              admin_root        /admin(.:format)                    admin/dashboard#index
         admin_dashboard        /admin/dashboard(.:format)          admin/dashboard#index
              admin_user PUT    /admin/users/:id(.:format)          admin/users#update
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
                         GET    /admin/users/:id(.:format)          admin/users#show
                         PUT    /admin/users/:id(.:format)          admin/users#update
                         DELETE /admin/users/:id(.:format)          admin/users#destroy
                    root        /                                   home#index

以下是日志的摘录

Started POST "/admin/users/batch_action" for 127.0.0.1 at 2013-01-18 23:07:07 +0530
Processing by Admin::UsersController#create as HTML

当路线显示时,为什么/admin/users/batch_action会被路由到Admin::UsersController#create batch_action_admin_users POST /admin/users/batch_action(.:format) admin/users#batch_action

我正在使用ActiveAdmin,这些是它生成的路由。

1 个答案:

答案 0 :(得分:0)

您似乎正在使用resources来管理管理员/用户控制器,因此POST http动词默认为控制器中的create操作。

如果您想添加另一个安静的POST控制器操作,请执行以下操作

scope "admin" do
  resources :users do
    member do
      post 'batch_action'
    end
  end
end

我不知道你的routes.rb是怎样的,所以你的routes.rb文件可能看起来不一样。但它应该看起来很相似。