仅使用设计和电子邮件注册电子邮件导轨

时间:2015-07-21 09:41:28

标签: 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

这些文章有问题吗?

1 个答案:

答案 0 :(得分:0)

为了在没有密码的情况下注册用户,我们要做的第一件事就是确保我们可以访问这样的设计视图

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>

现在表单将提交给控制器,但没有密码。这将触发错误密码不能为空

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

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