【问题标题】:RoR=> Strange validate messageRoR=> 奇怪的验证信息
【发布时间】:2010-12-07 03:24:00
【问题描述】:

我是 Rails 的新手,今天我使用 validate 制作了我的第一个 Web 应用程序,我只是将这行代码放入模型中:

class ClientWorkout < ActiveRecord::Base

  validates_numericality_of :paid_amount
  validates_presence_of :client_name

end

这是视图部分:

<% form_for(@client_workout) do |f| %>
  <%= f.error_messages %>
      etc etc

一切正常,并且值存储在数据库中,在成功的情况下

{{count}} errors prohibited this {{model}} from being saved

There were problems with the following fields:
{{attribute}} {{message}}
{{attribute}} {{message}}

(该示例显示了当表单的 2 个参数错误时发生的情况,但在每种情况下都会发生这种情况)
它无法将“计数、模型、属性和消息”替换为实际值。 任何人都可以弄清楚发生了什么? 我使用 Ror 2.3.8 和 rails 1.8.7

【问题讨论】:

    标签: ruby-on-rails forms validation


    【解决方案1】:

    Rails 在 2.3 中引入了内置的国际化。您的问题是一个已知错误,其中包含一些 rails 和 i18n gem 的组合。如果您有 i18n gem 版本 0.5.0,请尝试降级到 0.4.2。如果您使用的是系统 gem:

    sudo gem uninstall i18n
    sudo gem install i18n -v 0.4.2
    

    如果您使用 RVM 来管理 Gem,则不需要 sudo 命令。

    【讨论】:

    • 谢谢你现在它可以工作了,但是......我无法安装 gem i18 0.4.2,关键是一旦我卸载了 0.5.0 版,验证消息开始以正确的方式运行,但是现在,我没有 gem i18n,因为版本 0.4.2 只是假安装,而对于假安装,我的意思是:peppe@ubuntu:~$ sudo gem install i18n -v 0.4.2 成功安装 i18n-0.4.2 1 个 gem 安装 为 i18n-0.4.2 安装 ri 文档... 为 i18n-0.4.2 安装 RDoc 文档... peppe@ubuntu:~$ gem list | grep i18n peppe@ubuntu:~$
    【解决方案2】:

    如果您对更改 i18n 版本不感兴趣,可以执行以下操作 在config/locales/en.ym中添加以下代码

    如果en: 已经可用,则从 ActiveRecord 复制和粘贴

    然后停止服务器并重新启动应该正确显示错误消息...

    en:
      activerecord: 
        errors: 
          full_messages: 
            format: "%{attribute} %{message}"
          messages:
            inclusion: "is not included in the list"
            exclusion: "is reserved"
            invalid: "is invalid"
            confirmation: "doesn't match %{attribute}"
            accepted: "must be accepted"
            empty: "can't be empty"
            blank: "can't be blank"
            too_long: "is too long (maximum is %{count} characters)"
            too_short: "is too short (minimum is %{count} characters)"
            wrong_length: "is the wrong length (should be %{count} characters)"
            not_a_number: "is not a number"
            not_an_integer: "must be an integer"
            greater_than: "must be greater than %{count}"
            greater_than_or_equal_to: "must be greater than or equal to %{count}"
            equal_to: "must be equal to %{count}"
            less_than: "must be less than %{count}"
            less_than_or_equal_to: "must be less than or equal to %{count}"
            other_than: "must be other than %{count}"
            odd: "must be odd"
            even: "must be even"
          template:
            header:
              one:    "1 error prohibited this %{model} from being saved"
              other:  "%{count} errors prohibited this %{model} from being saved
    

    【讨论】:

    • 太棒了。我只是想解决同样的问题。发现这是一个 i18n 问题,我记得在另一个项目中遇到过。但不知道如何处理与您发布的 .yml 文件中的 template:header:other 匹配的消息。超级有帮助。谢谢。并且比更改 gem 版本更好的解决方案,即使它解决了这个问题,也可能会产生意想不到的副作用。
    猜你喜欢
    • 1970-01-01
    • 2016-01-31
    • 2012-05-24
    • 2011-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多