blueduckyy
blueduckyy

Reputation: 211

Update username not working in Devise

I'm using Devise and Rails 4.

When I go to edit a user and modify the username, I hit update, but the new username is not modified in the DB. Updating the password and the e-mail do both work correctly, just the username seems to be affected. I found a similar question but to the best of my knowledge I don't have a before_save action that might be causing this.

When in the rails console this is what I see before I perform the update action when I do User.first:

    => #<User id: 1, email: "[email protected]", created_at: "2017-03-03 19:10:48", updated_at: "2017-05-18 00:52:28", username: "blueduckyy", avatar_file_name: nil, avatar_content_type: nil, avatar_file_size: nil, avatar_updated_at: nil>

When I perform the update action, I see this is the terminal window:

      Parameters: {"utf8"=>"✓", "authenticity_token"=>"tFn+73EVxXbaZ4jgVen1iGB5U2yllqbyf/G+nL0Xp+xyRxQQtS2Eq3WTkJi7b1PP/FjduC6kC3X5ERgErjA+Vg==", "user"=>{"username"=>"blueduckyy2"}, "commit"=>"Save"}

In the browser, I get a response that says the User has been updated successfully.

Then if I perform User.first again, I get the same as above.

This is what I have in my Application Controller:

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

And if relevant, my Registrations Controller that I'm overriding for devise:

    class Users::RegistrationsController < Devise::RegistrationsController

      protected

      def update_resource(resource, params)
        resource.update_without_password(params)
      end

      def after_update_path_for(resource)
        user_path(resource)
      end

    end

And my user model:

    class User < ApplicationRecord
      # Include default devise modules. Others available are:
      # :confirmable, :lockable, :timeoutable and :omniauthable
      devise :database_authenticatable, :registerable,
             :recoverable, :rememberable, :trackable, :validatable

             validates :username, presence: true, length: { maximum: 15 }, uniqueness: {case_sensitive: false}

       has_attached_file :avatar, :styles => { :medium => "300x300>",:small => "200x200", :thumb => "100x100" }, :default_url => "/images/:style/missing.png"
       validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/

end

Upvotes: 2

Views: 1436

Answers (3)

Aathityan K
Aathityan K

Reputation: 1

This actually worked on rails 7 too! while adding it to the application controller.

def configure_devise_permitted_parameters

devise_parameter_sanitizer.permit(:sign_up, keys: [:username])
devise_parameter_sanitizer.permit(:account_update, keys: [:username])

end

Upvotes: 0

A relatively clean solution (that works on Rails 6) is to add this to your application_controller:

  before_action :configure_devise_permitted_parameters, if: :devise_controller?

  def configure_devise_permitted_parameters,
    devise_parameter_sanitizer.permit(:sign_up, keys: [:username])
    devise_parameter_sanitizer.permit(:account_update, keys: [:username])
  end

Upvotes: 1

Pedro Holguin
Pedro Holguin

Reputation: 66

In the registrations_controller.rb put this

class Users::RegistrationsController < Devise::RegistrationsController
 before_filter :configure_permitted_parameters

 def configure_permitted_parameters
  devise_parameter_sanitizer.permit(:sign_up) do |user_params|
   user_params.permit(:first_name, :last_name, :username,
    :email, :password, :password_confirmation)
  end
  devise_parameter_sanitizer.permit(:account_update) do |user_params|
   user_params.permit(:first_name, :last_name, :username,
    :email, :password, :password_confirmation, :avatar)
  end
 end
end

Upvotes: 1

Related Questions