Ruby on Rails中,若数据库无该邮箱,如何避免显示忘记密码错误提示?

更新于
2026-10-09 21:04:02
1阅读来源:SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计723个文字,预计阅读时间需要3分钟。

Ruby on Rails中,若数据库无该邮箱,如何避免显示忘记密码错误提示?

当用户点击忘记密码时,系统将引导他们至忘记密码页面,并提示输入电子邮箱地址。若电子邮箱地址在数据库中存在,一切顺利;若不存在,则提示电子邮箱地址不存在。

当用户点击忘记密码时,会将他们带到忘记密码页面,并带有一个字段供他们输入他们的电子邮件地址.如果他们的电子邮件地址在数据库中,一切都很好,但如果数据库中不存在电子邮件地址,它只会重定向到同一页面,但不会显示错误消息.

如何显示错误消息?

/view/devise/passwords/new.html.erb

... <%= form_for(resource, :as => resource_name, :url => password_path(resource_name), :html => { :method => :post }) do |f| %> <h1>Reset Password</h1> <div class="login-fields"> <p>Instructions on resetting your password will be emailed to you.</p> <%= render :partial => '/shared/messages' %> <div class="field"> <%= f.label :email %> <%= f.email_field :email, :placeholder => 'Email', :class => 'login username-field' %> </div> <!-- /field --> </div> <!-- /login-fields --> <div class="login-actions"> <%= content_tag(:button, :type=>:submit, :class => "button btn btn-secondary btn-large") do %> Send me reset password instructions <% end %> </div> <!-- .actions --> <div class="login-social"> <%= render "devise/shared/links" %> </div> <% end %> ...

/views/shared/_messages.html.erb

<% if alert || flash[:alert] || flash[:error] %> <div class="alert alert-error"> <a class="close" data-dismiss="alert" href="#">x</a> <h4 class="alert-heading">Error!</h4> <%= alert %> <%= flash[:error] %> </div> <% end %> <% if flash[:success] || notice || flash[:notice]%> <div class="alert alert-success"> <a class="close" data-dismiss="alert" href="#">x</a> <h4 class="alert-heading">Success!</h4> <%= flash[:success] %> <%= notice %> </div> <% end %> 在您的共享消息部分中,您只显示来自控制器的错误.您需要包含附加到模型的错误.在这种情况下,错误消息将附加到用户模型.你/views/shared/_messages.html.erb应该看起来像:

<% if alert || flash[:alert] || flash[:error] %> <div class="alert alert-error"> <a class="close" data-dismiss="alert" href="#">x</a> <h4 class="alert-heading">Error!</h4> <%= alert %> <%= flash[:error] %> </div> <% end %> <% if model.errors.any? %> <div class="alert alert-error"> <a class="close" data-dismiss="alert" href="#">x</a> <h4 class="alert-heading">Error!</h4> <ul> <% model.errors.full_messages.each do |msg| %> <li>* <%= msg %></li> <% end %> </ul> </div> <% end %> <% if flash[:success] || notice || flash[:notice]%> <div class="alert alert-success"> <a class="close" data-dismiss="alert" href="#">x</a> <h4 class="alert-heading">Success!</h4> <%= flash[:success] %> <%= notice %> </div> <% end %>

在渲染局部时,您需要传入模型.

Ruby on Rails中,若数据库无该邮箱,如何避免显示忘记密码错误提示?

<%= render '/shared/messages', model: resource %>

本文共计723个文字,预计阅读时间需要3分钟。

Ruby on Rails中,若数据库无该邮箱,如何避免显示忘记密码错误提示?

当用户点击忘记密码时,系统将引导他们至忘记密码页面,并提示输入电子邮箱地址。若电子邮箱地址在数据库中存在,一切顺利;若不存在,则提示电子邮箱地址不存在。

当用户点击忘记密码时,会将他们带到忘记密码页面,并带有一个字段供他们输入他们的电子邮件地址.如果他们的电子邮件地址在数据库中,一切都很好,但如果数据库中不存在电子邮件地址,它只会重定向到同一页面,但不会显示错误消息.

如何显示错误消息?

/view/devise/passwords/new.html.erb

... <%= form_for(resource, :as => resource_name, :url => password_path(resource_name), :html => { :method => :post }) do |f| %> <h1>Reset Password</h1> <div class="login-fields"> <p>Instructions on resetting your password will be emailed to you.</p> <%= render :partial => '/shared/messages' %> <div class="field"> <%= f.label :email %> <%= f.email_field :email, :placeholder => 'Email', :class => 'login username-field' %> </div> <!-- /field --> </div> <!-- /login-fields --> <div class="login-actions"> <%= content_tag(:button, :type=>:submit, :class => "button btn btn-secondary btn-large") do %> Send me reset password instructions <% end %> </div> <!-- .actions --> <div class="login-social"> <%= render "devise/shared/links" %> </div> <% end %> ...

/views/shared/_messages.html.erb

<% if alert || flash[:alert] || flash[:error] %> <div class="alert alert-error"> <a class="close" data-dismiss="alert" href="#">x</a> <h4 class="alert-heading">Error!</h4> <%= alert %> <%= flash[:error] %> </div> <% end %> <% if flash[:success] || notice || flash[:notice]%> <div class="alert alert-success"> <a class="close" data-dismiss="alert" href="#">x</a> <h4 class="alert-heading">Success!</h4> <%= flash[:success] %> <%= notice %> </div> <% end %> 在您的共享消息部分中,您只显示来自控制器的错误.您需要包含附加到模型的错误.在这种情况下,错误消息将附加到用户模型.你/views/shared/_messages.html.erb应该看起来像:

<% if alert || flash[:alert] || flash[:error] %> <div class="alert alert-error"> <a class="close" data-dismiss="alert" href="#">x</a> <h4 class="alert-heading">Error!</h4> <%= alert %> <%= flash[:error] %> </div> <% end %> <% if model.errors.any? %> <div class="alert alert-error"> <a class="close" data-dismiss="alert" href="#">x</a> <h4 class="alert-heading">Error!</h4> <ul> <% model.errors.full_messages.each do |msg| %> <li>* <%= msg %></li> <% end %> </ul> </div> <% end %> <% if flash[:success] || notice || flash[:notice]%> <div class="alert alert-success"> <a class="close" data-dismiss="alert" href="#">x</a> <h4 class="alert-heading">Success!</h4> <%= flash[:success] %> <%= notice %> </div> <% end %>

在渲染局部时,您需要传入模型.

Ruby on Rails中,若数据库无该邮箱,如何避免显示忘记密码错误提示?

<%= render '/shared/messages', model: resource %>