In validations, one can add a custom error message:> class Person < ActiveRecord::Base > def a_method_used_for_validation_purposes > errors.add(:name, "cannot contain the characters !@#%*()_-+=") > end > end >How wil this message be translated when using I18n? Can a key be passed instead of a message? Cheers, ace -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/Zuy9iksgPjMJ. For more options, visit https://groups.google.com/groups/opt_out.
Norbert Melzer
2012-Jul-22 09:47 UTC
Re: translations for custom validation error messages
Am 21.07.2012 02:21 schrieb "Ace Suares" <acesuares-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> > In validations, one can add a custom error message: > >> >> class Person < ActiveRecord::Base >> def a_method_used_for_validation_purposeserrors.add(:name, I18n.t(:thekey)) #"cannot contain the characters !@#%*()_-+=")>> end >> end > > > How wil this message be translated when using I18n? > Can a key be passed instead of a message?That should do it.> Cheers, > aceHth Norbert -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Thanks, Indeed, when a message is send as a String, it will be taken literally, and if it is send as a Symbol, it will be looked up in the translation table. Nifty! Cheers ace On Friday, July 20, 2012 8:21:50 PM UTC-4, Ace Suares wrote:> > In validations, one can add a custom error message: > > >> class Person < ActiveRecord::Base >> def a_method_used_for_validation_purposes >> errors.add(:name, "cannot contain the characters !@#%*()_-+=") >> end >> end >> > > How wil this message be translated when using I18n? > Can a key be passed instead of a message? > > Cheers, > ace > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/8ep45W1g3koJ. For more options, visit https://groups.google.com/groups/opt_out.