'Can't register user with Devise

I have model User and two controllers for manage users:

  1. Devise (without changes) - user can register and log in through Devise. Authorization works without problems,but registration does not work. Also the user cannot delete his account.
  2. admin_users controller - user management through the admin panel. Here everything works flawlessly, except for deleting the user.

Devise controller is original admin_users_controller

class Admin::UsersController < ApplicationController
  layout "admin_application"

  before_action :set_user, only: [:show, :edit, :update, :destroy]

  def index
    @users = User.where.not(:id => current_user.id).order("id ASC")
    #authorize @users
  end

  def show
    @user = User.find(params[:id])
    #authorize @user
  end

  def new
    @user = User.new
    #authorize @user
  end

  def create
    @user = User.new(user_params)
    #authorize @user
    if @user.save
      flash[:notice] = "Successfully created User."
      redirect_to admin_users_path
    else
      render :action => 'new'
    end
  end

  def edit
    @user = User.find(params[:id])
    #authorize @user
  end

  def update
    @user = User.find(params[:id])
    #authorize @user
    params[:user].delete(:password) if params[:user][:password].blank?
    params[:user].delete(:password_confirmation) if params[:user][:password].blank? and params[:user][:password_confirmation].blank?

    if @user.update(user_params)
      flash[:notice] = "Successfully updated User."
      redirect_to admin_users_path
    else
      render :action => 'edit'
    end
  end

  def destroy
      @user = User.find(params[:id])
      @user.destroy
      redirect_to admin_users_path
  end

  private

  def set_user
    @user = User.find(params[:id])
  end

  def user_params
     params.require(:user).permit(:avatar, :first_name, :middle_name, :last_name, :username, :email, :role_id, :birth_date, :password, :password_confirmation)
  end
end

ApplicationController class ApplicationController < ActionController::Base protect_from_forgery with: :exception include Pundit # Pundit authorisation

  add_flash_types :success, :danger, :info

  before_action :authenticate_user! # All users must be logged in redirect to lgin page

  before_action :configure_permitted_parameters, if: :devise_controller?

  protected
    def configure_permitted_parameters
      devise_parameter_sanitizer.permit(:sign_up, keys: [:avatar])
      devise_parameter_sanitizer.permit(:account_update, keys: [:avatar, :first_name, :middle_name, :last_name, :username, :birth_date])
    end
end

Links to delete in views

= link_to "Delete", user_path(current_user), data: { confirm: "Are you sure?" }, method: :delete, :class => "btn btn-danger"

and

= link_to "Delete", admin_user_path(@user), data: { confirm: "Are you sure?" }, method: :delete, :class => "btn btn-danger"

Console log when Devise registration:

Started POST "/users" for 127.0.0.1 at 2022-01-25 14:02:21 +0300
Processing by Devise::RegistrationsController#create as TURBO_STREAM
  Parameters: {"authenticity_token"=>"[FILTERED]", "user"=>{"email"=>"[email protected]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"}
  TRANSACTION (0.4ms)  BEGIN
  User Exists? (1.4ms)  SELECT 1 AS one FROM "users" WHERE "users"."email" = $1 LIMIT $2  [["email", "[email protected]"], ["LIMIT", 1]]
  TRANSACTION (0.2ms)  ROLLBACK
  Rendering layout layouts/application.html.haml
  Rendering devise/registrations/new.html.haml within layouts/application
  Rendered shared/_flash_messages.html.haml (Duration: 0.7ms | Allocations: 35)
  Rendered devise/shared/_error_messages.html.haml (Duration: 2.0ms | Allocations: 428)
  Rendered devise/registrations/new.html.haml within layouts/application (Duration: 16.8ms | Allocations: 1232)
  Rendered shared/_header.html.haml (Duration: 3.3ms | Allocations: 155)
  Rendered shared/_flash_messages.html.haml (Duration: 0.1ms | Allocations: 18)
  Rendered layout layouts/application.html.haml (Duration: 28.4ms | Allocations: 4102)
Completed 200 OK in 676ms (Views: 29.1ms | ActiveRecord: 1.9ms | Allocations: 8572)

And when ttry to delete account (GET method???):

Started GET "/users/0" for 127.0.0.1 at 2022-01-25 14:57:01 +0300
Processing by DashboardController#user as HTML
  Parameters: {"id"=>"0"}
  User Load (1.4ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 0], ["LIMIT", 1]]
  Rendering layout layouts/application.html.haml
  Rendering dashboard/user.html.haml within layouts/application
  ActiveStorage::Attachment Load (1.8ms)  SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = $1 AND "active_storage_attachments"."record_type" = $2 AND "active_storage_attachments"."name" = $3 LIMIT $4  [["record_id", 0], ["record_type", "User"], ["name", "avatar"], ["LIMIT", 1]]
  ↳ app/helpers/application_helper.rb:12:in `avatar'
  ActiveStorage::Blob Load (0.7ms)  SELECT "active_storage_blobs".* FROM "active_storage_blobs" WHERE "active_storage_blobs"."id" = $1 LIMIT $2  [["id", 5], ["LIMIT", 1]]
  ↳ app/helpers/application_helper.rb:13:in `avatar'
  Role Load (0.4ms)  SELECT "roles".* FROM "roles" WHERE "roles"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
  ↳ app/views/dashboard/user.html.haml:14
  Rendered dashboard/user.html.haml within layouts/application (Duration: 808.7ms | Allocations: 22183)
  Rendered shared/_header.html.haml (Duration: 0.9ms | Allocations: 289)
  Rendered shared/_flash_messages.html.haml (Duration: 0.1ms | Allocations: 18)
  Rendered layout layouts/application.html.haml (Duration: 1072.4ms | Allocations: 42813)
Completed 200 OK in 1481ms (Views: 1073.2ms | ActiveRecord: 35.9ms | Allocations: 54136)

Can enybody help me? Thanks



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source