Your validation error message is coming from your model.  In your
model, you have something like this:

class MyModel < ActiveRecord::Base

  ...

  validates_presence_of :msg, :message => "can't be blank"

  ...

end

When you submit a form you are validating that the message someone
enters in the column field you created in your database, which is
called "msg" actually has something in it (validates_presence_of).
Here, we're saying when someone submits a form and the "msg" field is
empty, give them this error:

<name of column> can't be blank

The name of the column here is "msg."  But you don't want to display
that in your error back to the client.  You want it to say something
nice like "Message can't be blank"  not "msg can't be blank."

So, in my method, we take the same model we have above and add the
humanize mapping.


class MyModel < ActiveRecord::Base

  ...

  validates_presence_of :msg, :message => "can't be blank"

  HUMANIZED_COLUMNS = {:msg => "Message"}

  def self.human_attribute_name(attribute)
    HUMANIZED_COLUMNS[attribute.to_sym] || super
  end

end

With that method, you're just mapping a hash of symbols (in this case
you just have one - :msg) and modifying the human_attribute_name
method that rails uses when displaying the validation error.  You see,
when the validation fails (the value for the field was not present) it
spits out the error containing the (humanized version) of the column
name with the :message you gave it.  The validation error is always
humanizing the column name, but it can't make much of "msg" so it
leaves it alone.  I always suggest giving column names (in your
database / model) when you set it up to be good column names it can
humanize.  But, if you have to have one like "msg," then you can work
around it when it tries to humanize it.

To break down the method just a little, "human_attribute_name" is a
method in ActionRecord, but it's present here since your class
"MyModel" inherits from ActionRecord::Base.  You are essentially
modifying it by "def"ining it here.  You're just saying, when you
"humanize" a column, for whatever reason, see if the column name is in
my hash "HUMANIZED_COLUMNS" and use that value instead of the column
name you're going to try to humanize.

Hope that helps,
Richard

On Sep 26, 4:52 pm, bgumbiker <bogumil.bial...@gmail.com> wrote:
> THanks Richard,
> Could you post any further usage example as I am quite new in Ruby and
> Rails and do not understand what happening below in your code. Is :msg
> anattributein the model?
> thanks,
> bogumbiker
>
> On Sep 26, 11:20 pm, richardun <richar...@gmail.com> wrote:
>
> > Actually, bgumbiker, I just posted this answer yesterday so a bit more
> > searching might have produced an 
> > answer.http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/...
>
> > If you used Ranjan's answer, you'll have to enter a specific custom
> > message for eachvalidationyou do.  If you use the solution I
> > outlined in that URL or below, you change the "humanization" version
> > of that column name so you can do validations as normal and when the
> > human name is used to display anerrormessage, it'll use the one you
> > mapped.
>
> > class Post < ActiveRecord::Base
>
> >   HUMANIZED_COLUMNS = {:msg => "Message"}
>
> >   def self.human_attribute_name(attribute)
> >     HUMANIZED_COLUMNS[attribute.to_sym] || super
> >   end
> > end
>
> > Richard
>
> > On Sep 26, 5:50 am, ranjan kumar <ranjankumar...@gmail.com> wrote:
>
> > > Try this
>
> > > class User < ActiveRecord::Base
> > >   validate do |user|
> > >     user.errors.add_to_base("This is my custom message") if user.
> > > attribute_name.blank?
> > >   end
> > > end
>
> > > On Sat, Sep 26, 2009 at 2:52 PM, bgumbiker 
> > > <bogumil.bial...@gmail.com>wrote:
>
> > > > Hello,
> > > > Any idea how to removeattributename fromvalidationerrormessages?
> > > > thanks,
> > > > bogumbiker
>
>
--~--~---------~--~----~------------~-------~--~----~
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@googlegroups.com
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to