Formatting ActiveRecord messages in Rails 2.x

March 29, 2009

Last week I was reading a chapter in a RailsSpaces book and because the book was written in 2007 the code was a bit outdated. The issue that I was running into was the author was using the sprintf() function to format a string that is part of the ActiveRecord error messages. This is a problem because the ActiveRecord uses error messages such as :

“is too short (minimum is {{count}} characters)”

where the word in the double curly braces is the placeholder for the value that the developer must replace. I first thought that I simply was not using the sprintf() function correctly until I discovered that the sprintf() was the wrong method to use in the first place. What should be used is a new class that was introduced to Rails in version 2 called I18n which is a new way of localizing the text in rails applications. The correct way of formatting those kinds of messages in Rails now is:

				:count=> some_value)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: