简体   繁体   中英

Unable to work with custom Devise controller

Unable to work with custom Devise controller

Working with Rails 6.1.3, ruby 3.0.0p0

rails routes

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

controllers > users > 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

trace

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" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 5], ["LIMIT", 1]]
16:42:45 web.1     |   User Load (1.2ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["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" = $1 AND "users"."id" != $2 LIMIT $3  [["email", "ibai@gmail.comtu"], ["id", 5], ["LIMIT", 1]]
16:42:45 web.1     |   User Update (1.4ms)  UPDATE "users" SET "email" = $1, "updated_at" = $2 WHERE "users"."id" = $3  [["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)

So I can sign up and update users correctly, but somehow Devise is still using the default controllers, and not the update method I indicated in registrations_controller.rb (proof of that is line 'puts "jarabe"' is not printed in my trace and same for the notice: "User was successfully updated.", it's not printed, but instead I see "Your account has been updated successfully"

You are only overriding routes for SessionsController and forgot to also specify your RegistrationsController . In your routes.rb change this line:

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

to:

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

Watch this related post

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM