Adding Username to Devise Rails 4

How to add username field to devise gem?

Answer is now outdated [ Valid for rails4 ]

I have done the same. Please follow these steps:

  1. rails generate migration add_username_to_users username:string:uniq

  2. rake db:migrate

  3. add attr_accessible :username

  4. in application_controller.rb:

    before_action :configure_permitted_parameters, if: :devise_controller?

    protected
    def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:username, :email, :password, :password_confirmation, :remember_me) }
    devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:login, :username, :email, :password, :remember_me) }
    devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:username, :email, :password, :password_confirmation, :current_password) }
    end
  5. in config/initializers if you want to replace email by usernname

    config.authentication_keys = [ :username ]
    config.case_insensitive_keys = [ :username ]
    config.strip_whitespace_keys = [ :username ]
  6. update the views.

Note if attr_accessible :usernamegives error try attr_accessor :username

Rails: When adding column :username to devise, username value still shows as nil in console when I create a new user

Your form submission is being sent to the Devise::RegistrationsController, which - by default, will only permit the following parameters: :email, :password and :password_confirmation. If you view the rails log while submitting this form, you should see an error like:

unpermitted attribute: :username

There are a few possible solutions outlined in the Devise README; the simplest solution in your case being:

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

protected

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

You can read more about strong parameters here in the rails docs.

On a separate note, I would suggest adding the following validation rule to your model:

class User < ApplicationRecord
validates :username, presence: true
end

You could also consider doing something like "if no username is supplied, set it to equal the email address".

Logging in with username using Devise with Rails 4

Remove validatable from the User model devise section - this option validates the presence, uniqueness and format of the email address. You'll have to manually add your own validations for password presence, confirmation and length, though.

Rails 4 + Devise Login with email or username and strong parameters

The problem is with the field "login", remove :authentication_keys => [:login] and add
attr_accessor :login in your User model.

Rails 4 & Devise - User's name is not added to database when new user created

Rails 4 uses strong parameters.

You have to permit the parameters which you want to pass through controller to model.
in other words direct association to attributes in model through params is now not allowed you have to specify if any parameter value is suppose to pass through controller to model.

You may found some help on this blog:
http://edgeapi.rubyonrails.org/classes/ActionController/StrongParameters.html

or as if we consider your problem you can either override the SessionsController for devise or use

devise_parameter_sanitizer(:sign_up)

more help can be found here: https://github.com/plataformatec/devise#strong-parameters



Related Topics



Leave a reply



Submit