设计可确认。如何删除电子邮件字段?

Devise Confirmable. How to remove email field?

为了发送新的确认指令,必须输入电子邮件。我想避免这种情况,因为我的用户在那一刻登录,所以不需要电子邮件询问。我只想向 current_user.email

发送新指令

我不想做这样的客户端事情:

= f.email_field :email, value: current_user.email, class: "hidden"

我需要一个服务器端解决方案。

谢谢大家!

根据 devise codebase,可以对用户调用发送确认电子邮件,如下所示:

user = User.find(1)
user.send_confirmation_instructions

所以您真的不需要从表单中获取电子邮件。

您可以访问设备方法,这应该有效。

查看文档 here

routes.rb

devise_for :users, controllers: { confirmations: "confirmations" }

正在查看

= link_to "resend confirmation", user_confirmation_path, data: { method: :post }

我最终得到了这个:

首先,覆盖设计控制器:

config/routes.rb

devise_for :users, controllers: { confirmations: "users/confirmations" }

controllers/users/confirmations_controller.rb

class Users::ConfirmationsController < Devise::ConfirmationsController
  def create
    redirect_to new_user_session_path unless user_signed_in?
    if current_user.confirmed?
      redirect_to root_path
    else
      current_user.send_confirmation_instructions
      redirect_to after_resending_confirmation_instructions_path_for(:user)
    end
  end
end

  protected

    # The path used after resending confirmation instructions.
    def after_resending_confirmation_instructions_path_for(resource_name)
      flash[:notice] = "Instructions sent successfully."            
      is_navigational_format? ? root_path (or whatever route) : '/'
    end    
end

然后从视图中删除电子邮件字段。

views/devise/confirmations/new.html.haml

= form_for(resource, as: resource_name, url: confirmation_path(resource_name), method: :post }) do |f|
  = f.submit "Resend confirmation instructions"

感谢大家的回答。