开发者

Is Rails 3 missing an error translation?

In my en.yml I have this:


en:
  errors:
    format: "%{message}"
    messages:
      blank:        "%{attribute} can't be blank"
      invalid:      "%{attribute} is invalid"
      too_short:    "%{attribute} is too short"
      too_long:     "%{attribute} is too long"
      wrong_length: "%{attribute} is the wrong length"
      taken:        "%{attribute}, {value}, is already taken"

And here's my User model so far:


    validates_presence_of       :username
    validates_uniqueness_of     :username
    validates_length_of         :username,  :within => 4..15
    validates_format_of         :username,  :with => /^\w+$/i

    validates_presence_of       :password
    validates_length_of         :password,  :within => 6..20

When I test random data, all error messages work g开发者_如何学运维reat, except for the validates_uniqueness_of, which returns the default 'has already been taken'

Thank you very much in advance.


shouldn't it be

taken: "%{attribute}, %{value}, is already taken"

with percent sign for value? I didn't know you could access value, but it makes sense, otherwise that could be username. I see that taken is the right key, but I still would try without {value} to see if it works.

At last or temporary fix I think you can pass a message in you model validation, something like this should work:

validates_uniqueness_of     :username, :mesage => "#{self.username} is already taken"

but of course you loose a lot of benefits.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜