0
votes

I am learning how to use the devise gem and I have run into a few issues, I am trying to use the 'login' parameter to login. However my app will not update the users login. Not through forms or even through the console.

In the console it just SELECTs the login, it doesn't actually update it:

current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: "2016-03-03 15:31:55", updated_at: "2016-03-03 15:34:21", provider: nil, uid: nil, login: nil>

2.2.1 :036 > u.login => "stormviper" 2.2.1 :037 > u.save!

(0.1ms) begin transaction User Exists (0.2ms) SELECT 1 AS one FROM "users" WHERE (LOWER("users"."login") = LOWER('stormviper') AND "users"."id" != 4) LIMIT 1 (0.1ms) commit transaction => true

And then after this is run, it doesn't even update the User's login, I have made some changes to the application_controller which I believe the issue to be all wrong, but I don't know:

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception
  before_action :authenticate_user!, if: :devise_controller?
  before_filter :configure_permitted_parameters, if: :devise_controller?

protected
  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:login, :email, :password, :password_confirmation, :remember_me) }
    devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:login, :password, :remember_me) }
    devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:login, :email, :password, :password_confirmation, :current_password) }
  end
end

I have also made a few changes to the user.rb:

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  attr_accessor :login
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable,
         :authentication_keys => [:login]
  validates :login,
  :presence => true,
  :uniqueness => {
    :case_sensitive => false
  }



  def self.find_for_database_authentication(warden_conditions)
    conditions = warden_conditions.dup
    if login = conditions.delete(:login)
      where(conditions.to_h).where(["lower(login) = :value OR lower(login) = :value", { :value => login.downcase }]).first
    elsif conditions.has_key?(:login) || conditions.has_key?(:login)
      where(conditions.to_h).first
    end
  end

How would I go about solving this? Thanks

1
Quick question, why are you using this before_filter :configure_permitted_parameters, if: :devise_controller? you can generate devise controller and override permitted parameters if necessary.Qubaish Bhatti
I was following a tutorial and this is what I followed on thereuser5198598
If you are learning, i suggest you to use this tutorial github.com/plataformatec/devise it has every thing and detail documented.Qubaish Bhatti

1 Answers

0
votes

if you want to add 'login' parameter you can simply override devise registration controller and define this

def configure_sign_up_params
   params.require(:user).permit( :email, :password, :password_confirmation, :login )
end