Alex Rivera | Logout

how to customize rails activerecord validation error message to show attribute value

Asked 2011-05-20T14:22:38.747
30

When a user tries to create a record with a name that already exists, I want to show an error message like:

name "some name" has already been taken

I have been trying to do:

validates_uniqueness_of :name, :message => "#{name} has already been taken"

but this outputs the table name instead of the value of the name attribute

Edit
Report

1 Answer

3

What version of Rails do you use?

If Rails 3. then as i understand you should use :message => '%{value} has already been taken'. I am not sure about Rails 2.3. - but in any case you can create your own custom validation that performs what you need.

answered 2011-05-20T14:38:41.267

Your Answer