gpt4 book ai didi

ruby-on-rails - 冲突的 Devise 和 restful Controller 路由 | rails 5.2

转载 作者:数据小太阳 更新时间:2023-10-29 08:56:50 25 4
gpt4 key购买 nike

我正在开发一个使用 Devise 管理 User 模型的 Rails 5.2 项目。我希望用户能够自行注册,并允许管理员代表他们创建帐户。我已经使用 Devise 设置了 User 模型,并为资源生成了作用域 Controller (和 View )。这涵盖了自注册方面,并涵盖了管理员发起的注册,我创建了一个 users_controller 作为标准的 restful Controller ,并在路由中分别设置了一个资源路由。这似乎适用于除 users#create 操作路由与 devise_forusers/registrations#create 创建的路由冲突之外的所有内容(请参阅下面生成的路由)。这会导致 users#new 表单提交被 Devise 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

最佳答案

像这样

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

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

关于ruby-on-rails - 冲突的 Devise 和 restful Controller 路由 | rails 5.2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52108670/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com