← Kirby Week 2010: Welcome to Rails 3

Kirby Week 2010: Exploiting The Inflector

On the second day of Kirby Week, Four Island gave to me: a tutorial on pretty error messages.

Disclaimer: The post assumes you have Rails 3. It may work with Rails 2, but I dun know about that. Just to be safe, go ahead and install Rails 3. :P We good? Great.

Hi, 'yall! It's me! Do you know what's sort of annoying? Rails is really wonderful with all the things you can do with models. Specifically, validations are just great, and most of the time, they're really pretty too (because Rails is about Readability!), so you can write beautiful code like this:

validates_presence_of [:anon_name, :anon_email], :if => :anonymous?

You see? In one line, I made it so that the model checked for the presence of "anonname" and "anonemail" when "anonymous?" returned true. That's just great. However, there's a problem with this that you may be able to foresee. Let's see what kind of error messages are returned when "anonname" and "anonemail" are left out:

Anon Name is blank Anon Email is blank

That's not really that user friendly, is it. Oh well, we can just change our code a bit, right? Make it a bit less pretty?

validates_presence_of :anon_name, :message => "Your name is blank", :if => :anonymous?
validates_presence_of :anon_email, :message => "Your email is blank", :if => :anonymous?

It's a bit longer and clunkier, but it should work, right? Nope.

Anon Name Your name is blank Anon Email Your email is blank

D'oh! After searching around the Internet, the only solution I could find was this disgusting jumble:

validate do |comment|
  if comment.anonymous?
    comment.errors.add_to_base("Your name is blank") if comment.anon_name.blank?
    comment.errors.add_to_base("Your email is blank") if comment.anon_email.blank?
  end
end

Now the error messages are correct, but the code itself is horrendously disgusting. I can't settle for that! Well, it turns out that there's another way. A way that works with the first method of validation shown above, the one that validated both fields in one line. Yes. Go back to that. Next, look inside your config/initializers folder for a file called "inflections.rb". Add the following to the bottom:

ActiveSupport::Inflector.inflections do |inflect|
  inflect.human "anon_name", "Your username"
  inflect.human "anon_email", "Your email"
end

Now, restart your server and check your error messages. Guess what?

Your username is blank Your email is blank

This works because the inflector is used to humanize the names of fields before they are prepended to the error message. By customizing how the inflector humanizes the field names, you can effectively customize your error messages! Horray! The inflector can be a good ally once you learn how to use it properly because Rails humanizes things in a lot of places. Let the inflector be with you. Okay. I should end this post now. :P

Hatkirby on December 14th, 2010 at 12:30:07pm
👍 0 👎

Comments

Replying to comment by :
Feel free to post a comment! You may use Markdown.