知道为什么在这种情况下消息哈希为空吗?
我有这些验证:
validates_presence_of :part_number
validates_uniqueness_of :part_number
一个简单的创建:
if @part.save
puts 'saved'
redirect_to new_v2_path
else
puts 'not saved'
flash[:error] = "There was an error while updating the part."
redirect_to new_v2_path(@part)
end
在 View 中:
<% if @part.errors.any? %>
<div id="error_explanation" style="color: red;">
<h2><%= pluralize(@part.errors.count, 'error') %> :</h2>
<ul>
<% @part.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
错误对象:
...l, flush_part: nil>, @messages={}>
最佳答案
在失败的验证分支中,您将重定向而不是渲染,而是对:new_2
操作生成新请求并实例化一个新的@part
对象,但未分配任何值,因此会出现错误。您需要改用render
。
更改此行:
redirect_to new_v2_path(@part)
对此:
render :new # or new_v2 or whatever action name you have.
关于ruby-on-rails - 验证错误消息散列为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28459080/