冲突的设计和宁静的控制器路线| Rails 5.2

时间:2018-08-31 04:54:35

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

我正在一个使用Devise管理User模型的Rails 5.2项目中。我希望用户能够注册自己,并允许管理员代表他们创建帐户。我已经使用Devise建立了User模型,并为该资源生成了作用域控制器(和视图)。这涵盖了自注册方面,并且为了涵盖管理员启动的注册,我创建了一个users_controller作为标准的Res​​tful控制器,并在路由中分别设置了资源路由。这似乎适用于所有情况,除了users#create动作路由与devise_forusers/registrations#create创建的路由发生冲突(请参阅下面生成的路由)。这将导致users#new而不是users/registrations#create正确地处理users#create表单提交。

使*#create路由唯一的最佳方法是什么?

routes.rb

Rails.application.routes.draw do
  devise_for :users, controllers: { sessions: 'users/sessions', registrations: 'users/registrations' }

  resources :users
end

生成的路线

        new_user_session GET    /users/sign_in(.:format)        users/sessions#new
            user_session POST   /users/sign_in(.:format)        users/sessions#create
    destroy_user_session DELETE /users/sign_out(.:format)       users/sessions#destroy
       new_user_password GET    /users/password/new(.:format)   devise/passwords#new
      edit_user_password GET    /users/password/edit(.:format)  devise/passwords#edit
           user_password PATCH  /users/password(.:format)       devise/passwords#update
                         PUT    /users/password(.:format)       devise/passwords#update
                         POST   /users/password(.:format)       devise/passwords#create
cancel_user_registration GET    /users/cancel(.:format)         users/registrations#cancel
   new_user_registration GET    /users/sign_up(.:format)        users/registrations#new
  edit_user_registration GET    /users/edit(.:format)           users/registrations#edit
       user_registration PATCH  /users(.:format)                users/registrations#update
                         PUT    /users(.:format)                users/registrations#update
                         DELETE /users(.:format)                users/registrations#destroy
                         POST   /users(.:format)                users/registrations#create
---
                   users GET    /users(.:format)                users#index
                         POST   /users(.:format)                users#create
                new_user GET    /users/new(.:format)            users#new
               edit_user GET    /users/:id/edit(.:format)       users#edit
                    user GET    /users/:id(.:format)            users#show
                         PATCH  /users/:id(.:format)            users#update
                         PUT    /users/:id(.:format)            users#update
                         DELETE /users/:id(.:format)            users#destroy

3 个答案:

答案 0 :(得分:2)

类似这样的东西

devise_for :users, controllers: { sessions: 'users/sessions', registrations: 'users/registrations' }

devise_scope :user do
  post   '/users/create'  => 'users#create',  as: :create_user
  get   '/users'  => 'users#index',  as: :list_users
  get   '/users/:id' => 'users#show', as: :show_user
end

或者您可以在定义create action时跳过resources :users, except: :create,并使用registrations_controller create

答案 1 :(得分:0)

您可以configure the devise route进行区分(例如“ auth”)

routes.rb

Rails.application.routes.draw do
  devise_for :users, path: 'auth', controllers: { sessions: 'users/sessions', registrations: 'users/registrations' }
  resources :users
end

答案 2 :(得分:0)

您需要this

  devise_for :users, path: 'auth', controllers: {
    sessions: 'web/users/sessions',
    registrations: 'web/users/registrations'
  }