Reputation: 351
i'm adding a full_name (string) value to my model User, using gem Devise.
# app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
include Authorization
protect_from_forgery with: :exception
end
And also
# app/controllers/concerns/Authorization.rb
module Authentication
extend ActiveSupport::Concern
private
def devise_parameter_sanitizer
if resource_class == User
User::ParameterSanitizer.new(User, :user, params)
else
super
end
end
end
# app/controllers/sanitizers/user/parameter_sanitizer.rb
class User
class ParameterSanitizer < Devise::ParameterSanitizer
USER_PARAMS = %i(
full_name
email
password
password_confirmation
).freeze
def sign_up
default_params.permit(USER_PARAMS)
end
def account_update
default_params.permit(USER_PARAMS)
end
end
end
Everything should work, but I've got an error when creating user
Unpermitted parameter: full_name
Any ideas?
Upvotes: 1
Views: 550
Reputation: 2309
I believe the problem is that USER_PARAMS
is an array.
USER_PARAMS
# => [:full_name, :email, :password, :password_confirmation]
But you need to permit attributes like
permit(:full_name, :email, :password, ...)
So you can try to do
default_params.permit(*USER_PARAMS)
UPDATE
Check out devise source code.
It seems like you can use devise_parameter_sanitizer.permit
to allow additional keys for example for :sign_up
action.
Like
devise_parameter_sanitizer.permit(:sign_up, keys: [:full_name])
Note that devise already defines some DEFAULT_PERMITTED_ATTRIBUTES
so you don't need to redefine them.
So I think the following code should work for you
class ApplicationController < ActionController::Base
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:full_name])
devise_parameter_sanitizer.permit(:account_update, keys: [:full_name])
end
end
Upvotes: 1