Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Rails: Human Readable Error Messages

Posted on 2008-06-11
1
Medium Priority
?
1,204 Views
Last Modified: 2008-07-17
Is there a way to get Human Readable error messages from the Active Record object?  the default uses the database field name.  Publishing the errors to the web page would be much easier for the user to understand if it said "Last Name can not be blank" rather than "lname can not be blank".  Can this be done without a plugin?

Thanks,
Grant
0
Comment
Question by:renderbox
1 Comment
 
LVL 14

Accepted Solution

by:
wesgarrison earned 500 total points
ID: 21766018
You can roll your own messages, but the attribute names are humanized in the standard error messages.  
See code sample (1)
That's why it pays to name your field "last_name" instead of "lname"! You don't really save anything with short field names except a little typing in the migration and it's way paid off in situations like this.

I guess you could do something like code sample (2), if you needed.
(1)
Person.errors.add("first_name", "can't be blank")
Person.errors.full_messages # =>
    ["First name can't be blank"]
 
 
(2)
Person.errors.add("lname", "can't be blank")
err_pretty_names = { "lname" => "Last name", "dob" => "Date of birth", ...}
Person.errors.each do |attr|, |msg| 
  puts "#{err_pretty_names.include?(attr) ? err_pretty_names[attr] :  
    ActiveRecord::Base.human_attribute_name(attr)} - #{msg}"
end

Open in new window

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: narshlob
If you've ever programmed in Ruby and have come across either a proc or a lambda, you might have been wondering what the difference is between the two and when you would use one over the other. This article will try to explain the difference between…
Recently I spent hours debugging an issue in a Rails project where ActiveRecord was causing MySQL errors trying to create a User object of a class at the top level of a Single Table Inheritance model structure.  It turns out `.create` behaves differ…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Suggested Courses
Course of the Month12 days, 14 hours left to enroll

972 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question