Deprecation warning I get but the error is more than that I think

The following code from the RailsSpace book:
def correct_error_message(boundary, length)
error_messages = ActiveRecord::Errors.default_error_messages
if boundary == :max
sprintf(error_messages[:too_long], length)
elsif boundary == :min
sprintf(error_messages[:too_short], length)
else
raise ArgumentError, “boundary must be :max or :min”
end
end

resulted in the following error message.
Loaded suite /home/rmartin/NetBeansProjects/TestForge/test/unit/
user_test
Started
DEPRECATION WARNING: ActiveRecord::Errors.default_error_messages has
been deprecated. Please use I18n.translate
(‘activerecord.errors.messages’)… (called from default_error_messages
at /usr/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/
validations.rb:24)
…F…
Finished in 0.201547 seconds.

  1. Failure:
    test_screen_name_length_boundaries(UserTest)
    [/home/rmartin/NetBeansProjects/TestForge/test/unit/…/
    test_helper.rb:72:in assert_length' /home/rmartin/NetBeansProjects/TestForge/test/unit/user_test.rb: 39:intest_screen_name_length_boundaries’
    /usr/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/
    active_support/testing/setup_and_teardown.rb:94:in __send__' /usr/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/ active_support/testing/setup_and_teardown.rb:94:inrun’]:
    <“is too short (minimum is {{count}} characters)”> expected but was
    <“is too short (minimum is 4 characters)”>.

10 tests, 31 assertions, 1 failures, 0 errors

The deprecation warning I get but it doesn’t appear as though the
error message is being manipulated correctly; any help out there?

On Jan 9, 1:08 am, bearblu [email protected] wrote:

end
…F…
/usr/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/
active_support/testing/setup_and_teardown.rb:94:in `run’]:
<“is too short (minimum is {{count}} characters)”> expected but was
<“is too short (minimum is 4 characters)”>.

10 tests, 31 assertions, 1 failures, 0 errors

The deprecation warning I get but it doesn’t appear as though the
error message is being manipulated correctly; any help out there?

I am getting the same problem

The deprecation warning is easy

in your user_test.rb setup method replace the line:
@error_messages = ActiveRecord::Errors.default_error_messages
with the line:
@error_messages = I18n.translate(‘activerecord.errors.messages’)

This is part of the internationalization of rails.

I still haven’t tracked down why sprintf wants a variable named count
instead of just taking the variable in min_length.
For now I have performed the hack of commenting out the original line
and adding a hand formatted line for the two min_length tests:

#correct_error_message = sprintf(@error_messages[:too_short],

min_length)
correct_error_message = “is too short (minimum is #{min_length}
characters)”

and replace the three max_length tests:

#correct_error_message = sprintf(@error_messages[:too_long],

max_length)
correct_error_message = “is too long (maximum is #{max_length}
characters)”

I would prefer to use the sprintf to format the message from
the :too_short symbol. It should change automatically if the
default :too_short message changes. I’ll live with the hack for now
and chase down the change in sprintf in my spare time.

I’ll try to remeber to post the sprintf answer when i find it.

Sean

Well, it took me a while but I finally figured out the details of
this.

First: As mentioned before this is caused by support for
internationalization (Very Very cool feature) built into Rails 2.2.
Read more (and do read it, or your 2.2 projects will not progress too
far) at: rails.info

Second: First thing that needs to be done is to tell Rails where to
find the “dictionary” for Active Record error messages, and (if you
choose to use a locale other than en-US) also tell it to use the
errors in our chosen locale. If you are using English, this is already
done for you. Otherwise, read the URL above.

Third: Next you need to choose your message, and have i18n library do
the translation and parameter filling for you. So all you need to do
is to replace this line:
correct_error_message = sprintf(@error_messages[:too_long],
max_length)
with this line:
correct_error_message = I18n.translate
“activerecord.errors.messages.too_long”, :count=>max_length
That is it!
You may ask, why should the internationalization gem do the parameter
replacement? Think about it! The plural form of a noun, or the formate
of date and time, or the symbols for currencies, even how numbers are
written, all vary from language to language. So it’s best to fill the
parameters right here.

Good Luck