ecs*_*ecs 5 devise ruby-on-rails-4
我正在使用Rails 4.0和Devise 3.0.0.rc.
我安装了Devise的默认设置 - 即使用您的电子邮件地址注册/使用.但是,我希望用户能够使用用户名而不是他们的电子邮件地址进行注册.我根据要更改的参数来跟踪railscast,以及我可以在这里找到的任何建议,我可能需要为Rails 4更改.但是,每当我尝试创建新用户时,我都会收到错误'电子邮件不能为空白'.有没有人知道造成这种情况的原因,以及我如何解决这个问题?谢谢!!
码:
应用控制器:
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
Run Code Online (Sandbox Code Playgroud)
登录(注册表单几乎相同):
<%= 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>
Run Code Online (Sandbox Code Playgroud)
在devise.rb配置中:
config.authentication_keys = [ :username ]
Run Code Online (Sandbox Code Playgroud)
用户控制器:
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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
validatable从User模型devise部分中删除- 此选项验证电子邮件地址的存在,唯一性和格式.但是,您必须手动添加自己的密码存在,确认和长度验证.
| 归档时间: |
|
| 查看次数: |
3107 次 |
| 最近记录: |