Rails 4 + Devise登录电子邮件或用户名和强大的参数 [英] Rails 4 + Devise Login with email or username and strong parameters

查看:138
本文介绍了Rails 4 + Devise登录电子邮件或用户名和强大的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很喜欢RoR,并坚持这个设计问题。
我想允许用户使用电子邮件或用户名登录(用用户名注册已经可以)。



我遵循这些文章:
第1条第2条,您可以看到以下结果:



application_controller.rb



  class ApplicationController< ActionController :: Base 
protect_from_forgery with::exception
before_filter:configure_permitted_pa​​rameters,如果::devise_controller?

protected

def configure_permitted_pa​​rameters
devise_parameter_sanitizer.for(:sign_up){| u | u.permit(:username,:email,,password)}
devise_parameter_sanitizer.for(:sign_in){| u | u.permit(:login,:password,:remember_me)}
end
end



user.rb



  class User< ActiveRecord :: Base 
devise:database_authenticatable,:可注册,:可恢复,可记忆,可追踪,可验证,:authentication_keys => [:login]

validates_uniqueness_of:username
validates_presence_of:username
validates:username,length:{in:4..20}

def self .find_first_by_auth_conditions(warden_conditions)
conditions = warden_conditions.dup
如果login = conditions.delete(:login)
其中(conditions).where([lower(username)=:value OR lower (email)=:value,{:value => login.downcase}])首先
else
其中(条件).first
end
end
end



new.html.erb



 <%= form_for(resource,:as => resource_name,:url => session_path(resource_name))do | f | %GT; 
< div><%= f.label:login,伪电子邮件%>< br />
<%= f.text_field:login,:autofocus => true%>< / div>
...



devise.rb



...
config.authentication_keys = [:login]
...



结果



 显示/home/action/workspace/rchq/app/views/devise/sessions/new.html.erb第5行上升的位置:

未定义的方法`login'for#< User:0x000000033996a0>

我不明白为什么它不起作用,因为我指定了sign_in许可证:

解决方案

问题在于login字段,删除:authentication_keys => ;在您的用户模型中添加
attr_accessor:login


I'm new to RoR and stuck with this devise problem. I want to allow users to sign in with email OR username (registration with username is already ok).

I followed these articles: Article 1 and Article 2 and you can see the result below:

application_controller.rb

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception
  before_filter :configure_permitted_parameters, if: :devise_controller?

  protected

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

user.rb

class User < ActiveRecord::Base
    devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :authentication_keys => [:login]

  validates_uniqueness_of :username
  validates_presence_of :username
  validates :username, length: { in: 4..20 }

  def self.find_first_by_auth_conditions(warden_conditions)
    conditions = warden_conditions.dup
    if login = conditions.delete(:login)
      where(conditions).where(["lower(username) = :value OR lower(email) = :value", { :value => login.downcase }]).first
    else
      where(conditions).first
    end
  end      
end

new.html.erb

<%= form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f| %>
  <div><%= f.label :login, "Pseudo ou email" %><br />
  <%= f.text_field :login, :autofocus => true %></div>
...

devise.rb

... config.authentication_keys = [ :login ] ...

result

Showing /home/action/workspace/rchq/app/views/devise/sessions/new.html.erb where line #5 raised:

undefined method `login' for #<User:0x000000033996a0>

I don't understand why it doesn't work because I specified that for "sign_in" permit :login for User.

解决方案

The problem is with the field "login", remove :authentication_keys => [:login] and add attr_accessor :login in your User model.

这篇关于Rails 4 + Devise登录电子邮件或用户名和强大的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆