Ruby on rails Rails-大写的电子邮件地址会导致错误,无论我如何尝试
我有一个相对简单的Rails应用程序,它的表单使用Desive gem。表单不需要用户名,但需要电子邮件地址。一切正常,当用户的电子邮件地址都是小写时,页面会引导用户进入下一页。但是,当用户输入字符串中带有大写字符的电子邮件地址时,我从Rails收到以下错误:Ruby on rails Rails-大写的电子邮件地址会导致错误,无论我如何尝试,ruby-on-rails,ruby,email,devise,Ruby On Rails,Ruby,Email,Devise,我有一个相对简单的Rails应用程序,它的表单使用Desive gem。表单不需要用户名,但需要电子邮件地址。一切正常,当用户的电子邮件地址都是小写时,页面会引导用户进入下一页。但是,当用户输入字符串中带有大写字符的电子邮件地址时,我从Rails收到以下错误: NoMethodError at /users undefined method `uuid' for nil:NilClass RegistrationsController#create app/controllers/regist
NoMethodError at /users
undefined method `uuid' for nil:NilClass
RegistrationsController#create
app/controllers/registrations_controller.rb, line 81
第81行(email:new\u user.email,
):
请求参数:
{"utf8"=>"✓",
"authenticity_token"=>"ySBGjHGNwiyeBqGh9nv0N5a8fIJO51bi1nIByev6a21Zh+ncMx5d99cTGKbKWpPvdYmGiBfLX9Gya0qmglrBWg==",
"plan"=>"10day-fmf",
"disc"=>"firstmonthfree10day",
"user"=>{"first_name"=>"asdf", "last_name"=>"asdfsadf", "email"=>"Fjsdlkjs@FLFklsd.com", "password"=>"lsdkjlfkjlskjdf"},
"commit"=>"TRY RISK FREE!",
"controller"=>"registrations",
"action"=>"create"}
--用大写字母记录电子邮件地址
我对这个问题进行了广泛的研究,发现它与电子邮件地址没有被正确地屏蔽有关。因此,根据我的研究,我在各种文件中添加了以下代码:
# Devise.rb
config.case_insensitive_keys = [:email]
然而,尽管我尽了最大努力将用户输入的电子邮件地址降下,大写字母仍然会导致错误!任何人能给我的任何帮助都将不胜感激!
谢谢尝试添加
。请在此处添加downcase
。new\u user=user。通过电子邮件查找(params[:user][:email]。downcase)
我假设您在数据库中存储了小写的电子邮件值,但在控制器的
params[:user][:email]
中存储了小写的电子邮件值Fjsdlkjs@FLFklsd.com". 这就是为什么User.find\u by_email(params[:User][:email])
返回nil
,并且不能对nil对象调用uuid
方法。为什么要手动检查电子邮件是否已经存在?我通常为你做那件事。(是否指定了良好的用户表名称
要降低控制器中的大小写,请降低可以执行的参数
before_action :downcase_email, if: -> { params[:user][:email].present? }
def downcase_email
params[:user][:email].downcase!
end
顺便说一句,您可以将此方法重构为check\u email
:
def check_email
return unless params[:email].present?
user = User.find_by_email(params[:email].downcase)
return 'taken' if Recurly::Account.find(user)
'available'
end
诸如此类。您不需要使用begin/rescue尽管我已经在上面标记了正确答案,但我想指出的是,一个小小的javascript onchange事件也解决了这个问题
<%= f.input_field :email, placeholder: "Email", id:"email", onchange: "this.value=this.value.toLowerCase();" %>
这可能会有帮助:显然你需要使用:
用户。查找\u以进行身份验证(电子邮件:params[:email])
而不是用户。where(电子邮件:email)
谢谢!这对我很有效,你的回答很有意义!
def check_email
return unless params[:email].present?
user = User.find_by_email(params[:email].downcase)
return 'taken' if Recurly::Account.find(user)
'available'
end
<%= f.input_field :email, placeholder: "Email", id:"email", onchange: "this.value=this.value.toLowerCase();" %>