removing attribute prefix from validation error messages

Hello, Any idea how to remove attribute name from validation error messages? thanks, bogumbiker

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

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/cf59e09f98981f27/6c7fe5ff9dd016bc?lnk=gst&q=customize+active+record+attribute+display+name+in+error+message#6c7fe5ff9dd016bc

If you used Ranjan's answer, you'll have to enter a specific custom message for each validation you 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 an error message, 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

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 an attribute in the model? thanks, bogumbiker

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

"Yes" is the short answer, but I was hoping that explaining more about it you'd get a little more out of my answer.

Cheers, Richard

Thanks Richard!, It is now clear I like it! bogum

You can use custom error message plugin to display custom message..

http://github.com/gumayunov/custom-err-msg/