# How to get the errors on Controlers

**URL:** <https://www.ruby-forum.com/t/how-to-get-the-errors-on-controlers/121986>\
**Category:** Rails\
**Created:** [December 3, 2007, 5:21pm UTC](https://www.ruby-forum.com/t/how-to-get-the-errors-on-controlers/121986 "2007-12-03T17:21:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/3bc359/32.png) [@Anson](https://www.ruby-forum.com/u/Anson)\
**Post date:** [December 3, 2007, 5:21pm UTC](https://www.ruby-forum.com/t/how-to-get-the-errors-on-controlers/121986/1 "2007-12-03T17:21:55Z")

</div>

Now i want to get the errors of Model(like some error of  
validate\_xxx\_of )

How can i do? it seems that no error object on Controllers by API  
docments,

Thanks.

---

<div class="post-metadata">

**Author:** ![Anson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/3bc359/32.png) [@Anson](https://www.ruby-forum.com/u/Anson)\
**Post date:** [December 3, 2007, 5:23pm UTC](https://www.ruby-forum.com/t/how-to-get-the-errors-on-controlers/121986/2 "2007-12-03T17:23:52Z")

</div>

ModelName.errors.add(attribute, message)  
ModelName.errors.add\_to\_base(message)

see:  
[http://api.rubyonrails.org/classes/ActiveRecord/Errors.html](http://api.rubyonrails.org/classes/ActiveRecord/Errors.html)

---

<div class="post-metadata">

**Author:** ![Anson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/3bc359/32.png) [@Anson](https://www.ruby-forum.com/u/Anson)\
**Post date:** [December 5, 2007, 1:31pm UTC](https://www.ruby-forum.com/t/how-to-get-the-errors-on-controlers/121986/3 "2007-12-05T13:31:28Z")

</div>

Anson wrote:

> Now i want to get the errors of Model(like some error of  
> validate\_xxx\_of )
> 
> How can i do? it seems that no error object on Controllers by API  
> docments,
> 
> Thanks.

object\_name.errors will give all these type errors.

Suppose if Student is the class name,  
@student = Student.new(params[:student])  
@student.save

In the above code, if there is any error while saving, you can get the  
errors in the controller like,

@student.errors

It will return the object of ActiveRecord::Errors class.

If you want to get the errors in array, you can use like,

@student.errors.full\_messages

Refer the ActiveRecord::Errors class in the ROR API

- Karthi

---

<div class="post-metadata">

**Author:** ![Anson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/3bc359/32.png) [@Anson](https://www.ruby-forum.com/u/Anson)\
**Post date:** [December 14, 2007, 10:59am UTC](https://www.ruby-forum.com/t/how-to-get-the-errors-on-controlers/121986/4 "2007-12-14T10:59:57Z")

</div>

I have 2 validations on a single object, like this in my user model:

validates\_length\_of :password, :within =\> 6…12  
validates\_presence\_of :password

When I do a unit test to ensure a missing password is responded to  
correctly, for example:

@error\_messages = ActiveRecord::Errors.default\_error\_messages  
user = User.new(:name =\> ‘my test user’,  
assert !user.valid?  
assert\_equal @error\_messages[:blank], user.errors.on(:password)

I get a message such as:

test\_create\_user\_passwords\_missing(UserTest)  
[test/unit/user\_test.rb:143]:  
\<“can’t be blank”\> expected but was  
\<[“is too short (minimum is 6 characters)”, “can’t be blank”]\>.

I have 2 questions:

1. Is there a way of ensuring this test passes, ie. by checking for both  
validations at once?
2. How can I surpress or change the display of these messages in the  
views? For example, if I get the “can’t be blank” and the “is too  
short…” message, I would like to display a message saying “Please  
ensure you enter a password between 6 and 12 characters in length”.

All help appreciated!

Thanks

Mark.

Karthi kn wrote:

> Anson wrote:
> 
> > Now i want to get the errors of Model(like some error of  
> > validate\_xxx\_of )
> > 
> > How can i do? it seems that no error object on Controllers by API  
> > docments,
> > 
> > Thanks.
> 
> object\_name.errors will give all these type errors.
> 
> Suppose if Student is the class name,  
> @student = Student.new(params[:student])  
> @student.save
> 
> In the above code, if there is any error while saving, you can get the  
> errors in the controller like,
> 
> @student.errors
> 
> It will return the object of ActiveRecord::Errors class.
> 
> If you want to get the errors in array, you can use like,
> 
> @student.errors.full\_messages
> 
> Refer the ActiveRecord::Errors class in the ROR API
> 
> - Karthi
