Rails 4/Devise/MongoDB: “Unpermitted parameters” using custom properties and strong parameters

前端 未结 4 1379
南方客
南方客 2021-01-02 19:36

Trying to add a nested custom attribute, Profile (a Mongoid document), to my devise User class. When the Devise registration form is submit

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-02 20:17

    I found a different method that allows all the devise overriding logic and code to reside in the application controller. This allows any and all custom params to be passed through for each devise action (sign in, sign up, update). I also add a parameter sanitizer for devise_invitable and handle that logic here (invite, accept_invitation). I've got custom params like avatar, avatar_cache, etc:

    #application_controller.rb
    
      before_filter :configure_permitted_parameters, if: :devise_controller?
    
    protected
      # There are just three actions in Devise that allows any set of parameters to be passed down to the model, 
      # therefore requiring sanitization. Their names and the permited parameters by default are:
    
      # sign_in (Devise::SessionsController#new) - Permits only the authentication keys (like email)
      # sign_up (Devise::RegistrationsController#create) - Permits authentication keys plus password and password_confirmation
      # account_update (Devise::RegistrationsController#update) - Permits authentication keys plus password, password_confirmation 
      # and current_password. More at https://github.com/plataformatec/devise#strong-parameters
    
      def configure_permitted_parameters
        devise_parameter_sanitizer.for(:accept_invitation) do |u|
          u.permit(:username,:validate_username, :password,:password_confirmation, :invitation_token)
        end
        devise_parameter_sanitizer.for(:invite) do |u|
          u.permit(:name,:comments)
        end
    
        devise_parameter_sanitizer.for(:sign_up) do |u|
          u.permit(:username,:password,:password_confirmation)
        end
        devise_parameter_sanitizer.for(:sign_in) do |u|
          u.permit(:username,:email,:password,:password_confirmation,:phone, :validate_username, :avatar_cache, :remove_avatar, :current_password,:remember_me)
        end
    
        devise_parameter_sanitizer.for(:account_update) do |u|
          u.permit(:username,:email,:password,:password_confirmation,:phone, :validate_username,:avatar, :avatar_cache, :remove_avatar, :current_password)
        end
      end
    

    Find and read more at https://github.com/plataformatec/devise#strong-parameters

提交回复
热议问题