Rails - 无法显示错误消息

发布于 2024-12-08 05:50:56 字数 1428 浏览 0 评论 0原文

当我发送错误的电子邮件时,验证无法通过,但视图中的错误消息不会显示:(

我在模型中:

validate :recipient_not_have_invitation, :notice => "That user have already invitation"

def recipient_not_have_invitation
  errors.add :notice, 'That user have already invitation' if InvitationToGroup.find_by_recipient_email_and_group_id(recipient_email, group_id)
end

在控制器中:

(...)

if @invitation_to_group.save
  Mailer.invitation_to_group(@invitation_to_group).deliver
  redirect_to root_url, :notice => "Successfully send invitation to user #{@invitation_to_group.recipient_email}"
else
  redirect_to new_invitation_to_group_path(:group_id =>  @invitation_to_group.group_id)
end

在视图中 (invitation_to_groups/new.html.erb)

<h2>New Invitation to group </h2>
<% if notice %>
  <p id="notice"><%= notice %></p>
<% end %>

<%= form_for @invitation_to_group do |f| %>
   <% if @invitation_to_group.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@invitation_to_group.errors.count, "error") %> prohibited this user from being invitation:</h2>

      <ul>
      <% @invitation_to_group.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

(...)

When I send wrong email, validation can't pass but error messages in views don't be displayed :(

I have in models:

validate :recipient_not_have_invitation, :notice => "That user have already invitation"

def recipient_not_have_invitation
  errors.add :notice, 'That user have already invitation' if InvitationToGroup.find_by_recipient_email_and_group_id(recipient_email, group_id)
end

in controller:

(...)

if @invitation_to_group.save
  Mailer.invitation_to_group(@invitation_to_group).deliver
  redirect_to root_url, :notice => "Successfully send invitation to user #{@invitation_to_group.recipient_email}"
else
  redirect_to new_invitation_to_group_path(:group_id =>  @invitation_to_group.group_id)
end

In views (invitation_to_groups/new.html.erb)

<h2>New Invitation to group </h2>
<% if notice %>
  <p id="notice"><%= notice %></p>
<% end %>

<%= form_for @invitation_to_group do |f| %>
   <% if @invitation_to_group.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@invitation_to_group.errors.count, "error") %> prohibited this user from being invitation:</h2>

      <ul>
      <% @invitation_to_group.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

(...)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我不吻晚风 2024-12-15 05:50:56

问题是您在控制器中的 else 之后使用了 redirect_to 。因此,您将执行新操作,在此操作中您有 InvitationToGroup.new。因此,您可以毫无错误地构建新对象;)您需要使用 render 方法 instesd。

附言。你真的应该考虑使用“formattastic”。它将改进您的视图中的代码。请观看这两个railscast:http://railscasts.com/episodes/184-formtastic-part -1http://railscasts.com/episodes/185-formtastic-part-2

The problem is that you are using redirect_to after else in your controller. So you are going to new action and in this action you have InvitationToGroup.new. So you build new object without errors ;) You need to use render method instesd.

PS. you should really consider using "formtastic". It will imporve your code in views. Please watch this two railscasts: http://railscasts.com/episodes/184-formtastic-part-1 and http://railscasts.com/episodes/185-formtastic-part-2

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文