Rails模型验证错误消息未显示在视图上

时间:2015-10-22 21:15:12

标签: ruby-on-rails-3 ruby-on-rails-4 ruby-on-rails-3.2 rails-activerecord

我正在尝试在视图中显示验证错误。当我尝试保存此错误时,会在视图和日志中引发错误,而不是使用我的自定义消息additional filter type is required 这是我的代码

模型

 class AdditionalFilter < ActiveRecord::Base

 validates :additional_filter_type, presence:{ message: "additional filter type is required" }
 end

查看

   <% if @form.errors.any? %>
        <%= @form.errors%>    <!-- i know should just display an array -->
   <%end%>

我正在使用Rails 4.任何帮助将不胜感激,甚至指导如何调试。谢谢你

以下是日志中的错误

 ActiveRecord::RecordInvalid - Validation failed: Additional filters   additional filter type additional filter type is required:
 activerecord (4.2.3) lib/active_record/validations.rb:79:in   `raise_record_invalid'
 activerecord (4.2.3) lib/active_record/validations.rb:43:in `save!'

1 个答案:

答案 0 :(得分:3)

您正在使用save!,这会引发异常,这将完全跳过您的观点。您应该使用save,并允许无效记录通过&#34;在你看来。