设计:将未经证实的电子邮件地址传回登录页面

当用户在我的Rails应用程序中注册帐户时,我使用默认的Devise行为向他们发送确认电子邮件。 在网站上,用户填写注册表后,他们会自动重定向到登录页面,并提醒他们需要通过电子邮件确认他们的帐户:

“请通过电子邮件确认您的帐户。”

我希望警报更具体,比如说

“确认电子邮件已发送至。请单击电子邮件中的链接以完成注册过程!”

如何将未经证实的电子邮件地址传回视图?

我想,当您创建用户时,在创建/保存用户时,您仍然在数据库中保存未经证实的电子邮件地址? 如果是这样,您应该可以像调用视图中的其他变量一样调用它。 确保它们在关联的控制器中定义,然后在视图中使用类似<%= @ user.email%>的方式调用它们。

需要覆盖设计注册控制器使用此代码创建操作:

class RegistrationsController < Devise::RegistrationsController # POST /resource def create build_resource(sign_up_params) if resource.save # this block will be used when user is saved in database if resource.active_for_authentication? # this block will be used when user is active or not required to be confirmed set_flash_message :notice, :signed_up if is_navigational_format? sign_up(resource_name, resource) respond_with resource, :location => after_sign_up_path_for(resource) else # this block will be used when user is required to be confirmed user_flash_msg if is_navigational_format? #created a custom method to set flash message expire_session_data_after_sign_in! respond_with resource, :location => after_inactive_sign_up_path_for(resource) end else # this block is used when validation fails clean_up_passwords resource respond_with resource end end private # set custom flash message for unconfirmed user def user_flash_msg if resource.inactive_message == :unconfirmed #check for inactive_message and pass email variable to devise locals message set_flash_message :notice, :"signed_up_but_unconfirmed", email: resource.email else set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" end end end 

然后在devise.en.yml文件中传递email变量

 en: devise: registrations: signed_up_but_unconfirmed: "A confirmation email has been sent to %{email}. Please click the link in the email to finish the registration process!"