Title: how to customize rails activerecord validation error message to show attribute value
Tags: ruby-on-rails;ruby;activerecord
Question: 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 =&gt; "#{name} has already been taken"
but this outputs the table name instead of the value of the name attribute

Here is the accepted answer: 2 things:
The validation messages use the Rails I18n style interpolation, which is %{value}
The key is value rather than name, because in the context of internationalization, you don't really care about the rest of the model.
So your code should be:
validates_uniqueness_of :name, :message =&gt; '%{value} has already been taken'
Comment for this answer: Nice! Definitely cleaner than my answer.
Comment for this answer: It may be useful to know that %{attribute} is also available for use.

Here is another answer: What version of Rails do you use?
If Rails 3. then as i understand you should use :message =&gt; '%{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.
Comment for this answer: for this application, i am using rails 2.3.4

Here is another answer: It looks like you can pass a Proc to the message. When you do this, you get two parameters:
A symbol along the lines of :activerecord.errors.models.user.attributes.name.taken
A hash that looks something like `{:model=>"User", :attribute=>"Name", :value=>"My Name"}
So if you allow for two parameters on a proc, you can use the attributes[:value] item to get the name that was used:
validates_uniqueness_of :name,
:message =&gt; Proc.new { |error, attributes|
"#{attributes[:value]} has already been taken."
}

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 3, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2011:00:05/20/11', 'FavoriteCount': 7.0, 'PostTypeId': 1, 'Tags': '<ruby-on-rails><ruby><activerecord>', 'Title': 'how to customize rails activerecord validation error message to show attribute value', 'ViewCount': 18071, 'id': 6073303, 'user_AboutMe': '<p>merge delete</p>\n', 'user_AccountId': 23501.0, 'user_DisplayName': 'umar', 'user_Reputation': 4261.0, 'user_Views': 345.0, 'user_id': 58948.0}}