无法使用自定义 Devise 控制器

Unable to work with custom Devise controller

无法使用自定义 Devise 控制器

使用 Rails 6.1.3,ruby 3.0.0p0

rails 路线

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)                                                                           devise/registrations#cancel
                   new_user_registration GET    /users/sign_up(.:format)                                                                          devise/registrations#new
                  edit_user_registration GET    /users/edit(.:format)                                                                             devise/registrations#edit
                       user_registration PATCH  /users(.:format)                                                                                  devise/registrations#update
                                         PUT    /users(.:format)                                                                                  devise/registrations#update
                                         DELETE /users(.:format)                                                                                  devise/registrations#destroy
                                         POST   /users(.:format)                                                                                  devise/registrations#create

控制器 > 用户 > registrations_controller.rb

class Users::RegistrationsController < Devise::RegistrationsController

  # GET /resource/sign_up
  # def new
  #   super
  # end

  # POST /resource
  # def create
  #   super
  # end

  # GET /resource/edit
  # def edit
  #   super
  # end

  def update
    
    respond_to do |format|
      if @user.update(user_params)
        format.html { redirect_to @user, notice: "User was successfully updated." }
        format.json { render :show, status: :ok, location: @user }
        puts "jarabe"
      else
        format.html { render :edit, status: :unprocessable_entity }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

end

routes.rb

require 'sidekiq/web'

Rails.application.routes.draw do
  get '/privacy', to: 'home#privacy'
  get '/terms', to: 'home#terms'
authenticate :user, lambda { |u| u.admin? } do
  mount Sidekiq::Web => '/sidekiq'

  namespace :madmin do
  end
end

  resources :notifications, only: [:index]
  resources :announcements, only: [:index]

  devise_for :users, controllers: { sessions: 'users/sessions' }
  root to: 'home#index'
  # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
end

追踪

Started PUT "/users" for ::1 at 2021-03-08 16:42:45 +0100
16:42:45 web.1     | Processing by Devise::RegistrationsController#update as HTML
16:42:45 web.1     |   Parameters: {"authenticity_token"=>"[FILTERED]", "user"=>{"email"=>"iba123i@gmail.comtu", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "current_password"=>"[FILTERED]"}, "commit"=>"Update"}
16:42:45 web.1     |   User Load (1.6ms)  SELECT "users".* FROM "users" WHERE "users"."id" =  ORDER BY "users"."id" ASC LIMIT   [["id", 5], ["LIMIT", 1]]
16:42:45 web.1     |   User Load (1.2ms)  SELECT "users".* FROM "users" WHERE "users"."id" =  LIMIT   [["id", 5], ["LIMIT", 1]]
16:42:45 web.1     |   TRANSACTION (18.6ms)  BEGIN
16:42:45 web.1     |   User Exists? (1.2ms)  SELECT 1 AS one FROM "users" WHERE "users"."email" =  AND "users"."id" !=  LIMIT   [["email", "ibai@gmail.comtu"], ["id", 5], ["LIMIT", 1]]
16:42:45 web.1     |   User Update (1.4ms)  UPDATE "users" SET "email" = , "updated_at" =  WHERE "users"."id" =   [["email", "ibai@gmail.comtu"], ["updated_at", "2021-03-08 15:42:45.811176"], ["id", 5]]
16:42:45 web.1     |   TRANSACTION (1.9ms)  COMMIT
16:42:45 web.1     | Redirected to http://localhost:5000/
16:42:45 web.1     | Completed 302 Found in 334ms (ActiveRecord: 26.1ms | Allocations: 9241)

所以我可以正确注册和更新用户,但不知何故 Devise 仍在使用默认控制器,而不是我在 registrations_controller.rb 中指出的更新方法(证据是第 'puts "jarabe"' 行没有打印在我的跟踪中,同样的通知:“用户已成功更新。”,它没有打印,而是我看到“您的帐户已成功更新”

您只是覆盖了 SessionsController 的路线,而忘了指定您的 RegistrationsController。在您的 routes.rb 中更改此行:

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

至:

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

看这个related post