NameError: uninitialized constant DeviseController
NameError: uninitialized constant DeviseController
我尝试将我的 Rails 应用程序从 6.1 升级到 7,在 运行 bundle app:update
之后,我收到以下错误:
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)
环境:
- Ruby 2.7.5
- Rails 7.0.2.4
- 设计 4.8.1
任何解决此问题的提示都会有所帮助,谢谢:)
编辑
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!
这可能是 devise & rails 7 的问题,无论如何,在我的一个项目中,我是这样设计的:
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
和在同一个文件中
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
无需创建 devise_permitted_parameters.rb
我尝试将我的 Rails 应用程序从 6.1 升级到 7,在 运行 bundle app:update
之后,我收到以下错误:
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)
环境:
- Ruby 2.7.5
- Rails 7.0.2.4
- 设计 4.8.1
任何解决此问题的提示都会有所帮助,谢谢:)
编辑
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!
这可能是 devise & rails 7 的问题,无论如何,在我的一个项目中,我是这样设计的:
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
和在同一个文件中 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
无需创建 devise_permitted_parameters.rb