How to add username field to devise gem?
Answer is now outdated [ Valid for rails4 ]
I have done the same. Please follow these steps:
rails generate migration add_username_to_users username:string:uniq
rake db:migrate
add
attr_accessible :username
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) }
endin config/initializers if you want to replace
email
byusernname
config.authentication_keys = [ :username ]
config.case_insensitive_keys = [ :username ]
config.strip_whitespace_keys = [ :username ]update the views.
Note if attr_accessible :username
gives 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 addattr_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
Need Advice: Is This a Good Use Case for a 'Nosql' Database? If So, Which One
Ruby Mechanize Post with Header
Controlling Tor Client with Ruby
API Errors Customization for Rails 3 Like Github API V3
Map an Array Modifying Only Elements Matching a Certain Condition
Split Array Up into N-Groups of M Size
Does Concurrency Happen Even When Only One Thread Is in a Thread Pool
Is There a Function Like String#Scan, But Returning Array of Matchdatas
How to Fix Undefined Method 'Split' for Nil:Nilclass Error
How to Make a Ruby Script Run Once a Second
Ruby - Create Singleton with Parameters
Openssl Error Installing Ruby 2.0.0-P195 on MAC with Rbenv
Dynamically Generate Scopes in Rails Models
Use Separate Authentication Model with Devise on Rails