You are here: Forums General Discussion Customizing error_messages, sh...
Posted in Forums : General Discussion
Authority 0
Posting Rating 0
Sign in to rate this post
|
I needed to fix the default error_messages mechanism which was bad in one particular instance, which lead to “Secret answers answer can’t be blank”. However, I did not want to reinvent or have to provide error messages for every validate, which were some of the solutions I found around the net. I started with an idea I read about overriding error_messages_for, which is what I did. I left it largely intact because I did not want to mess with the default behavior. You should be able to paste this function into any of your module helpers and anywhere you use <%= form.error_messages %> it should work exactly as it did before. Now for the changes… The header_message can be changed but I did not want to lose the count so you can provide any string you want and use the $COUNT place holder. This is not case sensitive. <%= f.error_messages :header_message => “my form had $COUNT errors” %> That is helpful but now for the major fix. If you prepend the :message text with an ! (exclamation point) it will take that string literally. If you do not prepend it with an ! it works as it always did. validates_presence_of :answer, :message => ’!Answers must be provided for each security question’ Still want to use the attribute name but not at the beginning of the string, then use the $attr place holder. validates_presence_of :answer, :message => ’!There were problems with $attr, so fix it!’ The code to paste… def error_messages_for(*params) options = params.extract_options!.symbolize_keys
|
General Discussion : Hanoi houses, apartments, villas and offices for lease
General Discussion : Sr. Backend ROR Engineer needed in Denver Metro Area.($120k)
General Discussion : Rails Developer($130k) needed in Denver, CO
General Discussion : House’ construction
General Discussion : Looking for full time Rails Web Developer
General Discussion : Free classified Directory (sadiqui999)
General Discussion : Louis Vuitton Damier Ebene brand Canvascheap special offer( www.salecheaplouisvuitton.com )
General Discussion : ROR Development
General Discussion : RoR programmers needed
General Discussion : Required Ruby on Rails Developer