Rails 4 + Devise 3.0.0 Adding Username

I am working with Rails 4 and Devise 3.0.0 and am new to using these new strong options. I added the username to the user model using the documentation on the wiki wiki . The problem I am facing is strong parameter changes in Rails 4.

How to add :login attribute to user model to enable login with username or email?

+7
source share
4 answers

From rails4 readme during development: https://github.com/plataformatec/devise/tree/rails4#strong-parameters

 class ApplicationController < ActionController::Base before_filter :configure_permitted_parameters, if: :devise_controller? protected def configure_permitted_parameters devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:login, :email) } end end 
+10
source

@ justin.chmura

This is the essence of how we ended up working. https://gist.github.com/AJ-Acevedo/6077336

Gist contains:
application / controllers / application_controller.rb
application / models / user.rb
config / initializers / devise.rb

+2
source

You have to make sure you turn on

 attr_accessor :login 

in the user model. Here I found a question explaining that attr_accessible is deprecated.

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

The difference between attr_accessor and attr_accessible

This is what my app / models / user.rb file looks like.

 class User < ActiveRecord::Base # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable attr_accessor :login def self.find_first_by_auth_conditions(warden_conditions) conditions = warden_conditions.dup if login = conditions.delete(:login) where(conditions).where(["username = :value OR lower(email) = lower(:value)", { :value => login }]).first else where(conditions).first end end validates :username, :uniqueness => { :case_sensitive => false } end 
+2
source

This will work just fine if you add the module to config/initializers , as indicated with all parameters,

The config/initializers/devise_permitted_parameters.rb with the following contents:

 module DevisePermittedParameters extend ActiveSupport::Concern included do before_filter :configure_permitted_parameters end protected def configure_permitted_parameters devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:username, :email, :password, :password_confirmation) } end end DeviseController.send :include, DevisePermittedParameters 
0
source

All Articles