简体   繁体   中英

Ruby on rails devise

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM