← Back to home

error_message_for (not error_messages_for)

March 12, 2008

Sometimes I want to print each error of a form right below the input/select box. And at the same time let the user know that there were problems.

If I use error_messages_for, I will be showing the user each error twice… not a good idea.

So for this case I wrote error_message_for, a method that tells you that there is something wrong whether you have 1 error or more. It also accepts more than one model.

def error_message_for(*args)
  "<p id='errorExplanation'>Oops, enter correct info.</p>" if args.any? {|o| error_messages_for(o).size > 0 }
end

Some usage examples are:

error_message_for :account
error_message_for :account, :user

← Back to home