Rails - 无法显示错误消息
当我发送错误的电子邮件时,验证无法通过,但视图中的错误消息不会显示:(
我在模型中:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是您在控制器中的
else
之后使用了redirect_to
。因此,您将执行新操作,在此操作中您有InvitationToGroup.new
。因此,您可以毫无错误地构建新对象;)您需要使用render
方法 instesd。附言。你真的应该考虑使用“formattastic”。它将改进您的视图中的代码。请观看这两个railscast:http://railscasts.com/episodes/184-formtastic-part -1 和 http://railscasts.com/episodes/185-formtastic-part-2
The problem is that you are using
redirect_to
afterelse
in your controller. So you are going to new action and in this action you haveInvitationToGroup.new
. So you build new object without errors ;) You need to userender
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