gpt4 book ai didi

ruby-on-rails - 使用设计和 rails 仅通过电子邮件注册

转载 作者:行者123 更新时间:2023-12-03 17:43:03 24 4
gpt4 key购买 nike

我正在尝试实现一个类似于 Medium 的用户身份验证系统,用户只需在注册时输入他们的电子邮件地址,他们就会在邮件中收到一个确认链接,点击该链接后,他们会被重定向回网站,然后被要求填写密码和其他详细信息。
我正在使用 Devise 并找到了 2 篇文章,但没有一篇是有效的。 Stackoverflow 发布了类似的问题,但没有好的解决方案。

https://github.com/plataformatec/devise/wiki/How-To:-Email-only-sign-up
https://github.com/plataformatec/devise/wiki/How-To:-Override-confirmations-so-users-can-pick-their-own-passwords-as-part-of-confirmation-activation

这些文章有问题吗?

最佳答案

为了在没有密码的情况下注册用户,我们要做的第一件事是确保我们可以访问这样的设计 View
rails generate devise:views
这个时候我最在意的观点是 app/views/devise/registrations/new.html.erb *但你一定要查看它们以确保它们与你的应用程序匹配*

你想找到并删除它。如果一切都是默认的,这应该是第 11 - 22 行

  <div class="field">
<%= f.label :password %>
<% if @minimum_password_length %>
<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>

现在表单将在没有密码的情况下提交给 Controller 。这将触发错误 密码不能为空

所以我现在要做的就是给它一个这样的密码

1) 复制设备 registrations_controller.rb进入您的应用程序
#app/controllers/devise/registrations_controller.rb
class Devise::RegistrationsController < DeviseController
...

def create
build_resource(sign_up_params)

resource.password = SecureRandom.hex(10) # sets the password

resource.save
yield resource if block_given?
if resource.persisted?
resource.send_reset_password_instructions # send them instructions how to reset password

if resource.active_for_authentication?
set_flash_message! :notice, :signed_up
sign_up(resource_name, resource)
respond_with resource, location: after_sign_up_path_for(resource)
else
set_flash_message! :notice, :"signed_up_but_#{resource.inactive_message}"
expire_data_after_sign_in!
respond_with resource, location: after_inactive_sign_up_path_for(resource)
end
else
clean_up_passwords resource
set_minimum_password_length
respond_with resource
end
end
...
end

我希望这个对你有用
你可以找到我的代码 here

关于ruby-on-rails - 使用设计和 rails 仅通过电子邮件注册,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31535526/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com