Solved

Rails: Human Readable Error Messages

Posted on 2008-06-11
1
1,083 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 14

Accepted Solution

by:
wesgarrison earned 125 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Pros and Cons of PHP 7 686
Disable one update action of puppet agent -test 6 257
git - conflict 5 224
Method Inheritance / Overriding in Ruby 5 179
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…
In Ruby, Call or invoke a API DLL library is easily via Win32API class, win32-api gem or other gems. For general DLL API call, there are quite a few references, some good tips list below: http://www.rubytips.org/2008/05/13/accessing-windows-api-fro…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

756 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