gpt4 book ai didi

ruby-on-rails - 设计 - 如果数据库中不存在电子邮件,则忘记密码不会显示错误消息

转载 作者:数据小太阳 更新时间:2023-10-29 07:23:49 24 4
gpt4 key购买 nike

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

如何让错误消息出现?

/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 %>

最佳答案

在您的部分共享消息中,您仅显示来自 Controller 的错误。您需要包括附加到您的模型的错误。在这种情况下,错误消息附加到用户模型。你/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 %>

在渲染部分时,您需要传入模型。

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

关于ruby-on-rails - 设计 - 如果数据库中不存在电子邮件,则忘记密码不会显示错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19717819/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com