# \[Question\] How to show models attributes' value in validation message?

**URL:** <https://www.ruby-forum.com/t/question-how-to-show-models-attributes-value-in-validation-message/149994>\
**Category:** Rails\
**Created:** [October 2, 2008, 2:07am UTC](https://www.ruby-forum.com/t/question-how-to-show-models-attributes-value-in-validation-message/149994 "2008-10-02T02:07:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![boblu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b19c9b/32.png) [@boblu](https://www.ruby-forum.com/u/boblu)\
**Post date:** [October 2, 2008, 2:07am UTC](https://www.ruby-forum.com/t/question-how-to-show-models-attributes-value-in-validation-message/149994/1 "2008-10-02T02:07:37Z")

</div>

Say I have a model with a validation in User model like this:

Class User \< ActiveRecord::Base  
validates\_uniqueness\_of :name,  
:message=\>“user has already exist!”  
end

As you can see, I want to write my own error message for this  
validation.  
And I get this error message in Controller like this:

Class Login \< ActiveController::Base  
def create\_user  
begin  
@user = User.new(:name=\>params[:name])  
@user.save!  
rescue  
if @user.errors.blank?  
flash.now[:notice\_err] = “Database error!”  
else  
temp = []  
@user.errors.each{|err| temmp \<\< err[1]}  
flash.now[:notice\_err] = “

- ”+temp.join("
- ")+"\</  
li\>

"  
end  
else  
flash.now[:notice] = “
- User #{params[:name]} created!\</  
li\>

”  
end  
end  
end

Everything goes on well for now.  
However, if I want to show the ‘error user name’ in the validation  
message like this

Class User \< ActiveRecord::Base  
validates\_uniqueness\_of :name,  
:message=\>“user #{self.name} has already  
exist!”  
end

It cannot pass, and the following error shows out:

undefined method ‘name’ in model

So my question is “How can I get the value of attributes in a model  
validation helper?”  
Are there anyone having any idea on this?

---

<div class="post-metadata">

**Author:** ![boblu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b19c9b/32.png) [@boblu](https://www.ruby-forum.com/u/boblu)\
**Post date:** [October 2, 2008, 8:42am UTC](https://www.ruby-forum.com/t/question-how-to-show-models-attributes-value-in-validation-message/149994/2 "2008-10-02T08:42:58Z")

</div>

boblu wrote:

> Class User \< ActiveRecord::Base  
> validates\_uniqueness\_of :name,  
> :message=\>“user #{self.name} has already  
> exist!”  
> end
> 
> It cannot pass, and the following error shows out:  
> undefined method ‘name’ in model

:message=\>"user #{name} has already  
or  
:message=\>"user #{attributes[:name] || @name} has already  
or  
write your own validation method

---

<div class="post-metadata">

**Author:** ![boblu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b19c9b/32.png) [@boblu](https://www.ruby-forum.com/u/boblu)\
**Post date:** [October 3, 2008, 1:52am UTC](https://www.ruby-forum.com/t/question-how-to-show-models-attributes-value-in-validation-message/149994/3 "2008-10-03T01:52:14Z")

</div>

Thank you for your reply.  
I have tried two of what your suggested.

":message=\>"user #{name} has already " shows “user has already”  
the #{name} will just be skipped though I had set a value in the model  
object.

":message=\>“user #{@name} has already” shows error message “@name not  
defined”

I will try this  
:message=\>"user #{attributes[:name]} has already  
and I will tell you the result here.

Anyway, thank you.

On Oct 2, 3:42 pm, Pokkai D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
