使用Devise with Rails登录用户名4 [英] Logging in with username using Devise with Rails 4

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

问题描述

我正在使用Rails 4.0和Devise 3.0.0.rc。



我已经使用默认设置(即注册/使用您的电子邮件地址)。但是,我希望用户可以使用用户名而不是其电子邮件地址注册。我已经跟踪轨道交通工具,要改变哪些参数,以及我可以在这里找到可能需要为Rails 4更改的任何建议。但是,每当我尝试创建一个新用户时,我收到错误'电子邮件不能为空白。有人有什么想法是什么导致这个,我该如何解决?谢谢!!



代码:



应用程序控制器:

  class ApplicationController< ActionController :: Base 
#通过引发异常来防止CSRF攻击。
#对于API,您可能需要使用:null_session。
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,:password,:password_confirmation)}
devise_parameter_sanitizer.for(:sign_in){| u | u.permit(:username,:password,,remember_me)}
end
end

登录(注册表格几乎相同):

 <%= form_for(资源,:as = resource_name,:url => session_path(resource_name))do | f | %GT; 
< div><%= f.label:username%>< br />
<%= f.text_field:username,:autofocus => true%>< / div>

在devise.rb配置中:

  config.authentication_keys = [:username] 

users controller: / p>

  private 
#使用回调在动作之间共享共同的设置或约束。
def set_user
@user = User.find(params [:id])
end

#永远不要相信来自可怕的互联网的参数,只允许白名单通过。
def user_params
params.require(:user).permit(:first_name,:middle_names,:last_name,:date_of_birth,:gender,:line_1,:line_2,:line_3,:town,:county, :邮政编码:contact_number,电子邮件,用户名,密码,密码确认,:remember_me)
结束

user.rb:

  protected 

def self.find_for_database_authentication(warden_conditions)
conditions = warden_conditions.dup
login = conditions.delete(:login)
其中(条件).where([lower(username)=:value,{:value => login .downcase}])。first
end


解决方案

用户模型 devise 部分删除可验证选项验证电子邮件地址的存在,唯一性和格式。不过,您必须手动添加自己的密码存在,确认和长度的验证。


I'm using Rails 4.0 and Devise 3.0.0.rc.

I installed Devise with its defaults - i.e. sign up/in using your email address. However, I'd like the users to be able to sign up with a username instead of their email address. I've followed the railscasts in terms of which parameters to change, plus any advice I can find on here for things I might need to change for Rails 4. However, whenever I try to create a new user, I am getting the error 'email can't be blank'. Does anyone have an idea as to what is causing this, and how I can fix it? Thanks!!

Code:

application controller:

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_filter :configure_permitted_parameters, if: :devise_controller?

  protected

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

Sign in (sign up form is pretty much the same):

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

In devise.rb config:

config.authentication_keys = [ :username ]

users controller:

private
    # Use callbacks to share common setup or constraints between actions.
    def set_user
      @user = User.find(params[:id])
    end

# Never trust parameters from the scary internet, only allow the white list through.
def user_params
  params.require(:user).permit(:first_name, :middle_names, :last_name, :date_of_birth, :gender, :line_1, :line_2, :line_3, :town, :county, :postcode, :contact_number, :email, :username, :password, :password_confirmation, :remember_me)
end

user.rb:

protected

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

解决方案

Remove validatable from the User model devise section - this option validates the presence, uniqueness and format of the email address. You'll have to manually add your own validations for password presence, confirmation and length, though.

这篇关于使用Devise with Rails登录用户名4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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