7

I'm working with Rails 4 and Devise 3.0.0 and am new to using these new strong paramters. I added a username to the User model using the documentation on the Devise wiki. The problem I'm running into is the strong parameters change in Rails 4.

How do I add the :login attribute to the user model to enable logging in with either the username or email?

Justin Chmura
  • 1,939
  • 1
  • 15
  • 17
  • Just to expand a bit, I would like to find a solution that I could use that didn't involve reusing the `protected_attributes` gem. If that's possible in this case. – Justin Chmura May 23 '13 at 14:15

4 Answers4

10

From the rails4 readme on devise: 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
Jesse Wolgamott
  • 40,197
  • 4
  • 83
  • 109
  • Nice! I guess I should have read the documentation a little bit more carefully. :) I added that and that seemed to help. Now I'm getting `undefined method 'login' for User`. I assume I still need to add something else for the sign in page to recognize the login attribute. – Justin Chmura May 24 '13 at 13:49
  • oh, sorry, you said add a "username" to the model. replace :login with :username above – Jesse Wolgamott May 24 '13 at 15:22
  • The `:login` attribute is just used on the login page to hold the username or email the user types in to login. I may have to just override the sessions controller to get that to work. – Justin Chmura May 24 '13 at 16:15
  • @justin.chmura let me know if there's anything else to help you with. Otherwise, please mark as accepted. – Jesse Wolgamott May 27 '13 at 12:49
  • @justin.chmura Did you ever get this working? Mind sharing the end solution? How did you end up overriding the sessions controller? – AJ. Jul 01 '13 at 17:49
  • 1
    @AJ. Honestly, I just reverted back to Rails 3 until Rails 4 gets a little bit more fleshed out. Everything I tried didn't work so I gave up for now :) – Justin Chmura Jul 01 '13 at 18:32
2

@justin.chmura

Here is a gist of how we ended up getting it working.
https://gist.github.com/AJ-Acevedo/6077336

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

AJ.
  • 1,248
  • 10
  • 27
2

You should make sure that you include the

attr_accessor :login

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

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

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
Community
  • 1
  • 1
Daniel
  • 2,950
  • 2
  • 25
  • 45
0

It will works fine if you add an module in config/initializers as followings with all parameters,

File config/initializers/devise_permitted_parameters.rb with 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
Rokibul Hasan
  • 4,078
  • 2
  • 19
  • 30