简体   繁体   English

Rails 5:使用用户名或电子邮件设计登录

[英]Rails 5: Devise login with username or email

I followed the tutorial on setting up devise to use an username or email and everything is working fine. 我按照设置设计的教程使用用户名或电子邮件,一切正常。 My problem lies in the layout, is there any way to display the form this way? 我的问题在于布局,有没有办法以这种方式显示表单?

I want to have only one field for both username and email and require only one of them. 我想只有一个字段用于用户名和电子邮件,只需要其中一个。

Username or Email:
[____________________]

Password:
[____________________]

[Sign In]

What would the actual code look like, since I never used mutliple attributes on a field. 实际代码会是什么样子,因为我从未在字段上使用过多属性。 Probably something like this, is this even possible ? 可能这样的事情,这甚至可能吗?

...
<%= f.text_field [:username, :email], class: "form-control" %>

You can't do this from the HTML form. 您无法从HTML表单执行此操作。 You'll need to add code to the User model to take the input and determine whether it matches an existing username or email. 您需要向User模型添加代码以获取输入并确定它是否与现有用户名或电子邮件匹配。 Fortunately, the Devise wiki has a detailed solution for what you need to do. 幸运的是, Devise wiki有一个详细的解决方案, 可以满足您的需求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM