Jes*_*ssa 6 ruby ruby-on-rails devise ruby-on-rails-4
我有一个简单的 Ruby on Rails 应用程序,使用 Devise 进行身份验证。我想让用户使用用户名而不是电子邮件登录,并尝试按照此处所述实现这一点: 设计使用用户名而不是电子邮件进行身份验证
扭曲的是我仍然希望每个用户都有一个电子邮件地址,并且在注册时需要一个。因此,我在注册表单上留下了“电子邮件”字段。
问题是,当我使用有效的电子邮件地址(或电子邮件的任何其他值)填写注册表单时,Devise 会出现错误:“电子邮件不能为空”。
为什么电子邮件被视为空白,我该如何解决?
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 :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:username, :email, :password, :password_confirmation, :remember_me) }
devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:username, :password, :remember_me) }
devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:username, :email, :password, :password_confirmation, :current_password) }
end
end
Run Code Online (Sandbox Code Playgroud)
用户模型
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
Run Code Online (Sandbox Code Playgroud)
查看注册
<h2>Sign up</h2>
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %>
</div>
<div class="field">
<%= f.label :username %><br />
<%= f.text_field :username %>
</div>
<div class="field">
<%= f.label :password %>
<% if @validatable %>
<em>(<%= @minimum_password_length %> characters minimum)</em>
<% end %><br />
<%= f.password_field :password, autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "off" %>
</div>
<div class="actions">
<%= f.submit "Sign up" %>
</div>
<% end %>
<%= render "devise/shared/links" %>
Run Code Online (Sandbox Code Playgroud)
devise.rb 修改为包括:
config.authentication_keys = [ :username ]
Run Code Online (Sandbox Code Playgroud)
这是提交表单时在 rails 服务器控制台中的输出:
Started POST "/users" for ::1 at 2015-03-17 14:29:13 -0700
Processing by Devise::RegistrationsController#create as HTML
Parameters: {"utf8"=>"?", "authenticity_token"=>"bc5+ffyBSr6h79aumdMpwHhp5OY69
Tk5oKyY+eIBHLCDDwPmxiMbjbE6OTIaeUPGLmS0J+QlwlGFGHki8SKsgA==", "user"=>{"email"=>
"jessa@example.com", "username"=>"Jessa", "password"=>"[FILTERED]", "password_co
nfirmation"=>"[FILTERED]"}, "commit"=>"Sign up"}
Unpermitted parameter: email
(0.0ms) BEGIN
(0.0ms) ROLLBACK
Rendered devise/shared/_links.html.erb (2.0ms)
Rendered devise/registrations/new.html.erb within layouts/application (11.0ms)
Run Code Online (Sandbox Code Playgroud)
我可以看到Unpermitted parameter: email,但不知道如何修复它。
有问题的代码是正确的并且有效。它可以用作使用用户名设计登录的工作示例,尽管有很多教程涵盖了这一点。
该错误是一个愚蠢的错误:文件意外地位于错误的位置。感谢 Evgeny Petrov,他在评论中提供的示例帮助我找到了差异。
| 归档时间: |
|
| 查看次数: |
3500 次 |
| 最近记录: |