Rails 模型验证错误消息未显示在视图上
Rails Model Validation error message is not displayed on the view
我试图在视图中显示验证错误。当我尝试保存此错误时,视图和日志中都会出现错误,而不是我的自定义消息 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. 任何帮助将不胜感激,甚至指导如何调试 this.Thank 你
这是日志中的错误
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!'
您正在使用 save!
,这会引发异常,这将完全跳过您的视图。您应该使用 save
,并允许无效记录 "fall through" 进入您的视图。
我试图在视图中显示验证错误。当我尝试保存此错误时,视图和日志中都会出现错误,而不是我的自定义消息 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. 任何帮助将不胜感激,甚至指导如何调试 this.Thank 你
这是日志中的错误
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!'
您正在使用 save!
,这会引发异常,这将完全跳过您的视图。您应该使用 save
,并允许无效记录 "fall through" 进入您的视图。