回报率=>奇怪的验证消息

时间:2010-12-07 03:24:01

标签: ruby-on-rails forms validation

我是rails的新手,今天我使用验证创建了我的第一个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

一切正常,价值存储在数据库中,成功案例&lt;如果出现错误,则会以下面这种奇怪的方式在视图上显示此错误:

{{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

2 个答案:

答案 0 :(得分:1)

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

sudo gem uninstall i18n
sudo gem install i18n -v 0.4.2

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

答案 1 :(得分:0)

如果您对更改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