Ans*_*arg 5 ruby-on-rails devise
我正在尝试实现一个类似于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
这些文章有问题吗?
为了在没有密码的情况下注册用户,我们要做的第一件事是确保我们可以访问这样的设计视图
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>
Run Code Online (Sandbox Code Playgroud)
现在表单将在没有密码的情况下提交给控制器。这将触发错误密码不能为空
所以我此时所做的就是给它一个像这样的密码
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
Run Code Online (Sandbox Code Playgroud)
我希望这可以帮助您在这里找到我的代码
| 归档时间: |
|
| 查看次数: |
505 次 |
| 最近记录: |