Forum: Ruby on Rails customize active record attribute display name in error message

7e7d298368b4aef581a5e176247bba02?d=identicon&s=25 drewB (Guest)
on 2009-09-25 23:50
(Received via mailing list)
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?
Aad37b5f7116c8d1f547d23b37566032?d=identicon&s=25 Greg Donald (destiney)
on 2009-09-26 00:33
(Received via mailing list)
On Fri, Sep 25, 2009 at 4:49 PM, drewB <dbatshaw@gmail.com> 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/Va...

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/
7e7d298368b4aef581a5e176247bba02?d=identicon&s=25 drewB (Guest)
on 2009-09-26 02:47
(Received via mailing list)
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"
1dc64af99efb447b9f3051865eccc597?d=identicon&s=25 richardun (Guest)
on 2009-09-26 03:31
(Received via mailing list)
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
1dc64af99efb447b9f3051865eccc597?d=identicon&s=25 richardun (Guest)
on 2009-09-26 03:31
(Received via mailing list)
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
1dc64af99efb447b9f3051865eccc597?d=identicon&s=25 Richard Navarrete (Guest)
on 2009-09-26 03:40
(Received via mailing list)
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
7e7d298368b4aef581a5e176247bba02?d=identicon&s=25 drewB (Guest)
on 2009-09-26 18:38
(Received via mailing list)
That is exactly what I was looking for!  Thank you!
Bc92bf303307ba7e4376213883e7a771?d=identicon&s=25 Tom Mac (tom-mac)
on 2010-02-03 13:59
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
Bc92bf303307ba7e4376213883e7a771?d=identicon&s=25 Tom Mac (tom-mac)
on 2010-02-04 04:38
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
Bc92bf303307ba7e4376213883e7a771?d=identicon&s=25 Tom Mac (tom-mac)
on 2010-02-04 05:30
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
> end


Thanks
Tom
48af402ac1a5a73b29d0d81438aaeddb?d=identicon&s=25 Sharagoz -- (sharagoz)
on 2010-02-04 11:34
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.
Bc92bf303307ba7e4376213883e7a771?d=identicon&s=25 Tom Mac (tom-mac)
on 2010-02-04 13:55
Hi
 Thanks for replying back.Can you please paste a good link explains how
to do this?

Tom
48af402ac1a5a73b29d0d81438aaeddb?d=identicon&s=25 Sharagoz -- (sharagoz)
on 2010-02-04 14:17
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
Dd2d775dea75b381edb1bbf0600a0907?d=identicon&s=25 Marnen Laibow-Koser (marnen)
on 2010-02-04 15:27
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.html

Unfortunately, Rails' official I18N is quite cumbersome.  I highly
recommend fast_gettext.

Best,
-- 
Marnen Laibow-Koser
http://www.marnen.org
marnen@marnen.org
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.