drewB
2009-Sep-25 21:49 UTC
customize active record attribute display name in error message
Is there an easy way to customize the display name of the attributed used in a validation error message. For example, let''s say I have an active record with the attribute :msg that is validated for presence. If is doesn''t exist I don''t want the user to see "msg can''t be blank!" I want it to say "Message can''t be blank!" Is there an easy way to do that?
richardun
2009-Sep-25 22:01 UTC
Re: customize active record attribute display name in error message
Hi drewB, You can change the "humanized" version of that symbol when it''s accessed via human_attribute_name. Try this: class Post < ActiveRecord::Base HUMANIZED_COLUMNS = {:msg => "Message"} def self.human_attribute_name(attribute) HUMANIZED_COLUMNS[attribute.to_sym] || super end end HTH, Richard On Sep 25, 4:49 pm, drewB <dbats...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Is there an easy way to customize the display name of the attributed > used in a validation error message. For example, let''s say I have an > active record with the attribute :msg that is validated for presence. > If is doesn''t exist I don''t want the user to see "msg can''t be > blank!" I want it to say "Message can''t be blank!" Is there an easy > way to do that?
richardun
2009-Sep-25 22:17 UTC
Re: customize active record attribute display name in error message
I responded just a bit ago, but I don''t see my response, so hopefully there won''t be two responses... Essentially, Drew, you can map the column to be something else when you call "humanize" on it. Try this: class Post < ActiveRecord::Base HUMANIZED_COLUMNS = {:msg => "Message"} def self.human_attribute_name(attribute) HUMANIZED_COLUMNS[attribute.to_sym] || super end end HTH, Richard On Sep 25, 4:49 pm, drewB <dbats...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Is there an easy way to customize the display name of the attributed > used in a validation error message. For example, let''s say I have an > active record with the attribute :msg that is validated for presence. > If is doesn''t exist I don''t want the user to see "msg can''t be > blank!" I want it to say "Message can''t be blank!" Is there an easy > way to do that?
Greg Donald
2009-Sep-25 22:31 UTC
Re: customize active record attribute display name in error message
On Fri, Sep 25, 2009 at 4:49 PM, drewB <dbatshaw-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Is there an easy way to customize the display name of the attributed > used in a validation error message. For example, let''s say I have an > active record with the attribute :msg that is validated for presence. > If is doesn''t exist I don''t want the user to see "msg can''t be > blank!" I want it to say "Message can''t be blank!" Is there an easy > way to do that?Have you read the validation documentation? All of the Rails validators support a :message option, for example look at validates_presence_of: http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#M002164 See where it says "message - A custom error message (default is: "can‘t be blank")." ? That means when you write validates_presence_of :foo, :message => "Message can''t be blank!" -- Greg Donald http://destiney.com/
drewB
2009-Sep-26 00:45 UTC
Re: customize active record attribute display name in error message
I am aware of the :message option. What I was hoping to do is be able to use many of the default messages and just change the attribute name displayed. For example, something like: attr_display_name :msg "Message" On Sep 25, 3:31 pm, Greg Donald <gdon...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Fri, Sep 25, 2009 at 4:49 PM, drewB <dbats...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Is there an easy way to customize the display name of the attributed > > used in a validation error message. For example, let''s say I have an > > active record with the attribute :msg that is validated for presence. > > If is doesn''t exist I don''t want the user to see "msg can''t be > > blank!" I want it to say "Message can''t be blank!" Is there an easy > > way to do that? > > Have you read the validation documentation? > > All of the Rails validators support a :message option, for example > look at validates_presence_of: > > http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMeth... > > See where it says "message - A custom error message (default is: > "can‘t be blank")." ? That means when you write > > validates_presence_of :foo, :message => "Message can''t be blank!" > > -- > Greg Donaldhttp://destiney.com/
Richard Navarrete
2009-Sep-26 01:18 UTC
Re: customize active record attribute display name in error message
I responded just a bit ago, but I don''t see my response, so hopefully there won''t be two responses... Essentially, Drew, you can map the column to be something else when you call "humanize" on it. Try this: class Post < ActiveRecord::Base HUMANIZED_COLUMNS = {:msg => "Message"} def self.human_attribute_name(attribute) HUMANIZED_COLUMNS[attribute.to_sym] || super end end HTH, Richard On Fri, Sep 25, 2009 at 4:49 PM, drewB <dbatshaw-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Is there an easy way to customize the display name of the attributed > used in a validation error message. For example, let''s say I have an > active record with the attribute :msg that is validated for presence. > If is doesn''t exist I don''t want the user to see "msg can''t be > blank!" I want it to say "Message can''t be blank!" Is there an easy > way to do that? > > >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
drewB
2009-Sep-26 16:36 UTC
Re: customize active record attribute display name in error message
That is exactly what I was looking for! Thank you! On Sep 25, 3:17 pm, richardun <richar...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I responded just a bit ago, but I don''t see my response, so hopefully > there won''t be two responses... > > Essentially, Drew, you can map the column to be something else when > you call "humanize" on it. Try this: > > class Post < ActiveRecord::Base > > HUMANIZED_COLUMNS = {:msg => "Message"} > > def self.human_attribute_name(attribute) > HUMANIZED_COLUMNS[attribute.to_sym] || super > end > end > > HTH, > Richard > > On Sep 25, 4:49 pm, drewB <dbats...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Is there an easy way to customize the display name of the attributed > > used in a validationerrormessage. For example, let''s say I have an > > active record with the attribute :msg that is validated for presence. > > If is doesn''t exist I don''t want the user to see "msg can''t be > > blank!" I want it to say "Messagecan''t be blank!" Is there an easy > > way to do that?
Tom Mac
2010-Feb-03 12:59 UTC
Re: customize active record attribute display name in error message
Hi I was just trying this My models and relations are as company has_many users user belongs_to company And when saving a user a company is also saved So in user model I have validates_associated :company The solution works perfectly for user model attributes. But it is not working for company attributes. For example in companies table i have address_city field And as the solution suggests I add to company model below code HUMANIZED_COLUMNS = {:address_city => "City"} def self.human_attribute_name(attribute) HUMANIZED_COLUMNS[attribute.to_sym] || super end But this is not working It shows Address city .But I need is City. Please help Thanks Tom -- Posted via http://www.ruby-forum.com/. -- 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 this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Tom Mac
2010-Feb-04 03:38 UTC
Re: customize active record attribute display name in error message
Hi One more thing is ,in order to show validates_associated error message than the regular message "is invalid" I am using the hack in environment.rb like module ActiveRecord::Validations::ClassMethods def validates_associated(*associations) associations.each do |association| class_eval do validates_each(associations) do |record, associate_name, value| associates = record.send(associate_name) associates = [associates] unless associates.respond_to?(''each'') associates.each do |associate| if associate && !associate.valid? associate.errors.each do |key, value| record.errors.add(key, value) end end end end end end end end -- Posted via http://www.ruby-forum.com/. -- 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 this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Tom Mac
2010-Feb-04 04:30 UTC
Re: customize active record attribute display name in error message
I could solve the problem the following way. But I dont know whether this the right approach.Please correct if wrong> module ActiveRecord::Validations::ClassMethods > def validates_associated(*associations) > associations.each do |association| > class_eval do > validates_each(associations) do |record, associate_name, > value| > associates = record.send(associate_name) > associates = [associates] unless > associates.respond_to?(''each'') > associates.each do |associate| > if associate && !associate.valid? > associate.errors.each do |key, value|humanized_columns = {:address_city => "City",:phone_oofice => "Office Phone Number"} record.errors.add(key, value,{:attribute => humanized[key.to_sym] || human_attribute_name(key.to_s)})> end > end > end > end > end > end > end > endThanks Tom -- Posted via http://www.ruby-forum.com/. -- 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 this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Sharagoz --
2010-Feb-04 10:34 UTC
Re: customize active record attribute display name in error message
I would just like to add that those who uses I18n can easily add these model "translations" to the localization file like so: en: activerecord: models: user: "User" company: "Company" attributes: user: login: "Username" password. "Password" company: name: "Company name" I prefer this method over having the HUMANIZED_COLUMNS in every model. I18n can also be used to customize error messages, if you dont like the default ones (like "is invalid"). This is certanly a lot cleaner than hacking active record. -- Posted via http://www.ruby-forum.com/. -- 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 this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Tom Mac
2010-Feb-04 12:55 UTC
Re: customize active record attribute display name in error message
Hi Thanks for replying back.Can you please paste a good link explains how to do this? Tom -- Posted via http://www.ruby-forum.com/. -- 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 this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Sharagoz --
2010-Feb-04 13:17 UTC
Re: customize active record attribute display name in error message
You mean how to use I18n? Ryan Bates has a screen cast on it: http://railscasts.com/episodes/138-i18n And then there''s the official I18n guide http://guides.rubyonrails.org/i18n.html -- Posted via http://www.ruby-forum.com/. -- 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 this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Marnen Laibow-Koser
2010-Feb-04 14:27 UTC
Re: customize active record attribute display name in error
Sharagoz -- wrote:> You mean how to use I18n? > Ryan Bates has a screen cast on it: > http://railscasts.com/episodes/138-i18n > And then there''s the official I18n guide > http://guides.rubyonrails.org/i18n.htmlUnfortunately, Rails'' official I18N is quite cumbersome. I highly recommend fast_gettext. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- 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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.