'NameError: uninitialized constant DeviseController

I have tried upgrading my Rails app from 6.1 to 7 and after running bundle app:update I get the following error:

rails aborted!
NameError: uninitialized constant DeviseController
/Users/martynas/projects/bend/config/initializers/devise_permitted_parameters.rb:17:in `<main>'
/Users/martynas/projects/bend/config/environment.rb:5:in `<main>'
Tasks: TOP => active_storage:update => environment
(See full trace by running task with --trace)

Environment:

  • Ruby 2.7.5
  • Rails 7.0.2.4
  • Devise 4.8.1

Any hints how to solve this would be helpful, thanks :)

Edit

devise_permitted_parameters.rb:

module DevisePermittedParameters
  extend ActiveSupport::Concern

  included do
    before_action :configure_permitted_parameters
  end

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up, keys:[:name])
    devise_parameter_sanitizer.permit(:account_update, keys: [:name])
  end

end

DeviseController.send :include, DevisePermittedParameters

environment.rb

# Load the Rails application.
require_relative "application"

# Initialize the Rails application.
Rails.application.initialize!


Solution 1:[1]

It could be an issue with devise & rails 7, anyway in one of my project i used devise this way:

application_controller.rb

class ApplicationController < ActionController::API
  before_action :configure_permitted_parameters, if: :devise_controller?

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.require(:account).permit(:sign_up, keys: %i[email password])
  end
end

and in the same file of account.rb,

class Account < ApplicationRecord
  devise :database_authenticatable, :registerable, :invitable,
         :recoverable, :rememberable, :confirmable, :validatable
     ......
end

class Account::ParameterSanitizer < Devise::ParameterSanitizer
  def initialize(*)
    super
    params.require(:user).permit(:account_update, keys: %i[password current_password])
    params.require(:user).permit(:register, keys: %i[password email])
  end
end

without the need to create devise_permitted_parameters.rb

Sources

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

Source: Stack Overflow

Solution Source
Solution 1 Joe