# Rails skips model validation

**URL:** <https://www.ruby-forum.com/t/rails-skips-model-validation/114836>\
**Category:** Rails\
**Created:** [September 17, 2007, 11:31pm UTC](https://www.ruby-forum.com/t/rails-skips-model-validation/114836 "2007-09-17T23:31:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![terry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b9e5f3/32.png) [@terry](https://www.ruby-forum.com/u/terry)\
**Post date:** [September 17, 2007, 11:31pm UTC](https://www.ruby-forum.com/t/rails-skips-model-validation/114836/1 "2007-09-17T23:31:04Z")

</div>

for the life of me, i cannot get validates\_presence\_of to return any  
errors, leaving the form blank and clicking submit fails on the create  
or save method, but no validation errors are rendered.

[http://pastie.caboo.se/98101](http://pastie.caboo.se/98101)

---

<div class="post-metadata">

**Author:** ![terry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b9e5f3/32.png) [@terry](https://www.ruby-forum.com/u/terry)\
**Post date:** [September 17, 2007, 11:34pm UTC](https://www.ruby-forum.com/t/rails-skips-model-validation/114836/2 "2007-09-17T23:34:04Z")

</div>

i should note that update\_attributes is working

---

<div class="post-metadata">

**Author:** ![terry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b9e5f3/32.png) [@terry](https://www.ruby-forum.com/u/terry)\
**Post date:** [September 17, 2007, 11:48pm UTC](https://www.ruby-forum.com/t/rails-skips-model-validation/114836/3 "2007-09-17T23:48:07Z")

</div>

On 9/17/07, terry [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> for the life of me, i cannot get validates\_presence\_of to return any  
> errors, leaving the form blank and clicking submit fails on the create  
> or save method, but no validation errors are rendered.
> 
> [Parked at Loopia](http://pastie.caboo.se/98101)

You’re getting confused as to how errors are reported.

When you write

\<%= error\_messages\_for :job %\>

It looks for a model object in the instance variable @job (like how  
form\_for works). The error messages are stored in the model object by  
a call to valid? (or save, create, etc.).

But you don’t have an @job variable, so error\_messages\_for doesn’t  
find anything.

Your controller code is wrong. First, create() will return a Job model  
object, even if it couldn’t be saved. So the “if” part is always true.

Second, you are doing a redirect after the create. But if you had an  
error, your error messages would be lost due to the redirect.

This should work:

@job = current\_user.jobs.build(params[:job])  
if @job.save  
flash[:notice] = “job has been posted”  
redirect\_to :action =\> ‘index’  
return  
end  
render :action =\> ‘new’

Note the render instead of redirect at the end. If the save fails, you  
need @job to be passed to the view, because it contains the error  
messages.

HTH

---

<div class="post-metadata">

**Author:** ![terry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b9e5f3/32.png) [@terry](https://www.ruby-forum.com/u/terry)\
**Post date:** [September 17, 2007, 11:54pm UTC](https://www.ruby-forum.com/t/rails-skips-model-validation/114836/4 "2007-09-17T23:54:07Z")

</div>

wow, awesome. thank you bob
