开发者

Why doesn't Rails' "errors.full_messages" replace attribute and message variables?

Having a strange problem with a rails model I just created.

Here are my validations:

validates_presence_of :from_name, :message => 'Please provide a from name.'
validates_presence_of :from_email
validates_presence_of :giftition_plan_id

I'm having issues using errors.full_messages as well as f.error_messages in my form:

g = Giftition.create
g.errors.first
=> ["from_name", "Please provide a from name."]
>> g.errors.full_messages
=> ["{{attribute}} {{message}}", "{{attribute}} {{message}}", "{{attribute}} {{message}}"]

I'm just getting "{{attribute}} {{message}}". Any ideas?

UPDATE: I've uninstalled rails 3 and all the gems that were installed with it and that 开发者_如何转开发made the problem go away. It's not a fix though... I would still like to have rails 3 installed.

UPDATE: Sounds like upgrading to 2.3.9 fixes the problem. Unfortunately, I've given up for now, but sometime in the future I will try that.


I ran into this problem as well with an old 2.3.5 Rails app that I inherited. I had the 5.0 version of the i18n gem installed. I saw that it needs the %{} syntax. Doing this in config/locales/en.yml did the trick:


en:
  activerecord:
    errors:
      full_messages:
        format: "%{attribute} %{message}"


Upgrading to Version rails 2.3.9 fixes this problem

gem install -v 2.3.9 rails --include-dependencies

EDIT:

You also need to edit the config\environment.rb file to change the RAILS_GEM_VERSION.

RAILS_GEM_VERSION = '2.3.9'

EDIT #2:

I should note that version 2.3.9 is not the latest version of the 2.3.X branch, and you should upgrade the the latest version available.


I fixed locally by removing i18n-0.5.0.

experimenting with i18n-0.4.0 yields (while returning the correctly interpolated string):

The {{key}} interpolation syntax in I18n messages is deprecated. Please use %{key} instead.
/usr/lib/ruby/gems/1.8/gems/i18n-0.4.0/lib/i18n/backend/base.rb:160:in `interpolate_without_deprecated_syntax'
/usr/lib/ruby/gems/1.8/gems/i18n-0.4.0/lib/i18n/backend/base.rb:155:in `gsub'
/usr/lib/ruby/gems/1.8/gems/i18n-0.4.0/lib/i18n/backend/base.rb:155:in `interpolate_without_deprecated_syntax'
/usr/lib/ruby/gems/1.8/gems/i18n-0.4.0/lib/i18n/backend/base.rb:188:in `preserve_encoding'
/usr/lib/ruby/gems/1.8/gems/i18n-0.4.0/lib/i18n/backend/base.rb:154:in `interpolate_without_deprecated_syntax'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/i18n_interpolation_deprecation.rb:21:in `interpolate'
/usr/lib/ruby/gems/1.8/gems/i18n-0.4.0/lib/i18n/backend/base.rb:48:in `translate'
/usr/lib/ruby/gems/1.8/gems/i18n-0.4.0/lib/i18n.rb:152:in `translate'
/usr/lib/ruby/gems/1.8/gems/i18n-0.4.0/lib/i18n/backend/base.rb:119:in `resolve'
/usr/lib/ruby/gems/1.8/gems/i18n-0.4.0/lib/i18n/backend/base.rb:104:in `default'
/usr/lib/ruby/gems/1.8/gems/i18n-0.4.0/lib/i18n/backend/base.rb:103:in `each'
/usr/lib/ruby/gems/1.8/gems/i18n-0.4.0/lib/i18n/backend/base.rb:103:in `default'
/usr/lib/ruby/gems/1.8/gems/i18n-0.4.0/lib/i18n/backend/base.rb:41:in `translate'
/usr/lib/ruby/gems/1.8/gems/i18n-0.4.0/lib/i18n.rb:152:in `translate'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/validations.rb:78:in `generate_message'

I guess 0.5.0 turns up the pain by outputting non-interpolated strings.


i18n is required by activesupport, so the way I got around loading the most recent version (0.5.0) is:

in config/preinitializer.rb ('secret' lifecycle hook that loads before activesupport):

require 'rubygems'
begin
  gem 'i18n', "~> 0.4.0"
rescue LoadError
  # no biggie, optional anyway
end


so i keep seeing upgrading rails as the solution to this

... or you can simply downgrade i18n to version 0.4

as outlined in this post

getting {{attribute}} {{message}} in RoR views


Go to dir_of_ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.x/lib/active_record/locale and edit the en.yml... replace the {{variable}} to %{variable}

Worked for me...

PS: maybe the path would be different for you. Adapt for you convenience.


cd <yourRailsProject> 
gem install i18n -v 0.4.0 -i vendor/ -V

to install the i18n gem into the vendor folder (-V for verbose output, just to see what's going on under the hood)


I'm working on a Rails 2.3.5 server at work that doesn't have i18n gem. The odd thing is that while my code works locally, I have this problem on the work server. And, another application I have with the same exact frozen gems does not display this problem on the work server.

I changed the activerecord en.yml file like above but also changed the actionpack en.yml file to get the error message title/header right:

   \vendor\rails\activerecord\lib\active_record\locale\en.yml

    #format: "{{attribute}} {{message}}"
     format: "%{attribute} %{message}"

    \vendor\rails\actionpack\lib\action_view\local\en.yml

      activerecord:
        errors:
          template:
            header:
              one:    "1 error prohibited this %{model} from being saved"
              other:  "%{count} errors prohibited this %{model} from being saved"     

Also, I didn't bother with it but if you have error messages that contain counts, it looks like in the activerecord en.yml file you'd need to change the syntax of some of the messages too (like):

#too_long: "is too long (maximum is {{count}} characters)"
too_long: "is too long (maximum is #{count} characters)"


Here's my complete config/locales/en.yml that solves the attribute, message, count, and model issues. I got this from the RoR docs here: http://guides.rubyonrails.org/i18n.html

en: activerecord: errors: full_messages: format: "%{attribute} %{message}" template: header: one: "1 error prohibited this %{model} from being saved" other: "%{count} errors prohibited this %{model} from being saved"

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜