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?