【发布时间】:2013-03-29 01:07:59
【问题描述】:
我正在尝试自定义用户在输入数据不正确时在表单顶部看到的错误消息警报。我尝试自定义的错误消息警报适用于嵌套形式的模型属性。
我尝试了解决方案 here,它说要编辑 config/locales/en.yml 文件,但这只会更改消息,而不是错误消息之前显示的模型和属性的名称。
我还尝试了比利在下面his answer 中的建议,结果相同。即
1 个错误禁止保存此远足路径:
- 来自“我的自定义空白错误消息”的路线指示
有没有办法让我在错误消息中显示对用户更友好的模型和属性名称,或者将它们完全从错误消息中删除?
这是我所拥有的:
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"
型号
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
【问题讨论】:
标签: ruby-on-rails-3 locale alert nested-forms nested-attributes