Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 220
tybro0103 Avatar asked Dec 06 '10 20:12

tybro0103


4 Answers

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}"
like image 154
Marty Haught Avatar answered Nov 20 '22 09:11

Marty Haught


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.

like image 23
Karl Avatar answered Nov 20 '22 10:11

Karl


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
like image 2
David Davis Avatar answered Nov 20 '22 11:11

David Davis


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

like image 1
carl crott Avatar answered Nov 20 '22 11:11

carl crott