gpt4 book ai didi

error-handling - 翻译自定义错误消息

转载 作者:行者123 更新时间:2023-12-03 07:47:54 25 4
gpt4 key购买 nike

我有一个表单(使用simple_form),我想实现对已翻译错误消息的支持。除错误消息外,我所有的翻译均会出现。

我的客户模型是:

class Customer < ActiveRecord::Base
attr_accessible :name, :phone, :email, :contact_method

validates_presence_of :phone, :email, :contact_method, :message => I18n.t(:required)
end

我的 fr.yml文件
fr:
name: 'Nom'
phone: 'Téléphone'
email: 'Courriel'
contact_method: 'Méthode de contact'
required: 'Requis'

我的表格如下:
= simple_form_for @customer do |f|
= f.input :name, label: t(:name)
= f.input :phone, label: t(:phone)
= f.input :email, label: t(:email)

有什么我想念的吗?

最佳答案

首先,您应该将Symbolvalidates_presence_of一起使用。不要使用I18n手动翻译它:

validates_presence_of :phone, :email, :contact_method, :message => :required

其次,将错误消息的翻译添加到您的语言环境文件中,如下所示:
activerecord:
errors:
models:
customer:
required: 'Requis'

关于error-handling - 翻译自定义错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12955208/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com