Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 带有性别属性的Rails错误消息_Ruby On Rails_Activerecord_Internationalization_Translation - Fatal编程技术网

Ruby on rails 带有性别属性的Rails错误消息

Ruby on rails 带有性别属性的Rails错误消息,ruby-on-rails,activerecord,internationalization,translation,Ruby On Rails,Activerecord,Internationalization,Translation,我正在为我的rails应用程序(pt.yml)编写一个翻译,用葡萄牙语翻译所有activerecord错误消息 activerecord: models: user: Usuário feedback: Contato attributes: user: password: "senha" phone_number: "celular" first_name: "nome" last_name: "s

我正在为我的rails应用程序(pt.yml)编写一个翻译,用葡萄牙语翻译所有activerecord错误消息

  activerecord:
    models:
      user: Usuário
      feedback: Contato
  attributes:
    user:
      password: "senha"
      phone_number: "celular"
      first_name: "nome"
      last_name: "sobrenome"
      password_confirmation: "Confirmação de senha"
    feedback:
      title: "Título"
      name: "Nome"
      message: "Mensagem"
    report:
      before_photo: "A foto do foco"
  errors:
    template:
      header:
        one: "1 erro impediu que o %{model.model_name.human} fosse salvo"
        other: "%{count} erros impediram que o %{model}  fosse salvo"
    messages:
      blank: "é obrigatório."
      confirmation: "não confere."
      empty: "é obrigatório."
      exclusion: "está reservado."
      invalid: "não é válido."
      taken: "já está em uso."
      too_long: "é muito longo. (mínimo de %{count} caracteres)"
      too_short: "é muito curto. (mínimo de %{count} caracteres)"
      not_a_number: "não é um número."

问题是属性可以是阳性的,也可以是阴性的,错误消息应该取决于它们的性别。例如,“奥布里加特里约或奥布里加特奥里亚”。当我们生成错误消息时,是否有一个简单的解决方案来处理属性的性别?

我意识到我可以为每个模型的每个属性使用不同的错误消息。config/locales/pt.yml看起来像这样:

pt:
    activerecord:
        models:
            user: Usuário
        attributes:
            password: "Senha"
            first_name: "Nome"
        errors:
            models:
                user:
                    attributes:
                        password:
                            blank: "é obrigatória."
                        first_name:
                            blank: "é obrigatório."

因此pt->errors->models->{model{u name}->attributes->{attribute{attribute{u name}将为每个属性指定这些自定义性别特定错误消息的位置。希望这对任何人都有帮助:)

这与其说是解决问题的正确方法,不如说是一种变通方法,但如果你不太担心的话,这可能是拉丁语衍生语言的一个好建议

我还努力使形容词与属性的性别匹配(英语中不存在这个问题),然后我发现我应该始终引用该字段:

Female attribute -> The field must not be blank
Male attribute -> The field must be whatever
这样,您就得到了一个干燥的er方法

特别是葡萄牙语,我的观点是:

  # ActiveRecord
  errors:
    messages:
      empty: "O campo é obrigatório."
      blank: "é campo obrigatório." # another idea

我面临着同样的问题,我认为没有一个简单的方法可以做到这一点。您可以运行自己的验证方法,然后根据性别添加错误消息。如果找到更好的方法,请告诉我:)