我正在尝试自定义表单顶部用户在输入数据错误时看到的错误消息提示。我想要自定义的错误消息提示是针对嵌套表单中的模型属性的。
我已经尝试了这里提供的解决方案,该方案建议编辑config/locales/en.yml
文件,但这仅更改了消息,未更改显示在错误消息之前的模型和属性名称。
我还尝试了Billy在下面提供的建议,但结果相同,即:
1 error prohibited this hikingtrail from being saved:
- Directions directions from 'My Custom Blank Error Message'
是否有一种方法可以在我的错误消息中显示更友好的模型和属性名称,或者从错误消息中完全删除它们?
以下是我的配置:
config/locales/en.yml
# Sample localization file for English. Add more files in this directory for other locales.
# See https://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.
en:
activerecord:
models:
direction: "In the Getting There section"
attributes:
direction:
directions_from: "From field"
errors:
full_messages:
format: "%{message}"
models:
direction:
attributes:
directions_from:
blank: "My Custom Blank Error Message"
Model
class Direction < ActiveRecord::Base
belongs_to :hikingtrail
attr_accessible :directions_by, :directions_desc, :directions_from
validates :directions_from, :presence => {message: "'My Custom Error Message'", :if => Proc.new { |a| a.directions_by? || a.directions_desc? } }
validates :directions_by, :presence => {message: "'My Custom Error Message'", :if => Proc.new { |a| a.directions_from? || a.directions_desc? } }
validates :directions_desc, :presence => {message: "'My Custom Error Message'", :if => Proc.new { |a| a.directions_from? || a.directions_by? } }
end
if
参数。查看此帖子以获取更多详细信息。 - Holly<% @hikingtrail.errors.messages.each do |msg, errors_array| %> <li><%= msg %></li> <% end %>
,但现在我收到了这个错误信息提示:directions.directions_from。 - Holly