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] = "<ul><li>"+temp.join("</li><li>")+"</
</ul>"
end else flash.now[:notice] = "<ul><li>User #{params[:name]} created!</
</ul>"
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?