我正在尝试自定义用户在错误输入数据时在表单顶部看到的错误消息警报。我尝试自定义的错误消息警报适用于嵌套形式的模型属性。
我在这里尝试了解决方案,它说要编辑config/locales/en.yml
文件,但这只会更改消息,而不是在错误消息之前显示的模型和属性的名称。
我还尝试了比利在下面的回答中提出的建议,结果相同。IE
1 个错误禁止保存此远足小径:
- 来自“我的自定义空白错误消息”的路线说明
有没有办法让我在错误消息中显示对用户更友好的模型和属性名称,或者将它们完全从错误消息中删除?
这是我所拥有的:
配置/语言环境/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"
模型
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