# Validations continued

**URL:** <https://www.ruby-forum.com/t/validations-continued/71534>\
**Category:** Rails\
**Created:** [February 21, 2006, 8:45pm UTC](https://www.ruby-forum.com/t/validations-continued/71534 "2006-02-21T20:45:16Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 21, 2006, 8:45pm UTC](https://www.ruby-forum.com/t/validations-continued/71534/1 "2006-02-21T20:45:16Z")

</div>

I simply can’t figure this out. I have been reading and re-reading Agile  
book and [wiki.rubyonrails.org](http://wiki.rubyonrails.org) - all sorts of validation methods and  
still, it doesn’t work.

Controller code

def create  
@client = Client.new(params[:client])  
if @client.save!  
flash[:notice] = ‘Client was successfully created.’  
redirect\_to :action =\> ‘list’  
else  
render :action =\> ‘new’  
end  
end

Model # includes the following

validates\_presence\_of :first\_name

protected  
def validate  
errors.add\_on\_empty %w(first\_name)  
end

And if I try to enter a new ‘client’ missing the first name, it sends me  
to trace error page but doesn’t capture it as it suggests in Agile book.

This is making me crazy and is a continuation of my issue from last  
night which a decent night sleep and breakfast has failed to clarify.

Help!

Craig

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 21, 2006, 9:13pm UTC](https://www.ruby-forum.com/t/validations-continued/71534/2 "2006-02-21T21:13:00Z")

</div>

Your working to hard. Simply leave the validates\_presence\_of in and  
get rid of the validate method.

The validate method is mainly used when you want to add some custom  
validation that isn’t covered by the supplied validates\_\* methods.

Also, you want to use @client.save (no !). The ! is what is actually  
sending out the exception, instead of just failing and adding the  
errors to the object.

HtH!  
-Nick

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 21, 2006, 9:22pm UTC](https://www.ruby-forum.com/t/validations-continued/71534/3 "2006-02-21T21:22:23Z")

</div>

Whats the stacktrace? If it says the error is in your view code, then  
thats where it is, and not in your controller.

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 21, 2006, 9:19pm UTC](https://www.ruby-forum.com/t/validations-continued/71534/4 "2006-02-21T21:19:19Z")

</div>

OK - but following your instructions and I have been there many  
times…I get sent to the trace error screen (not what I want) and it  
states that there is an error in my ‘view’ code “\_form.rhtml” on a  
meaningless line.

Craig

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 21, 2006, 9:59pm UTC](https://www.ruby-forum.com/t/validations-continued/71534/5 "2006-02-21T21:59:15Z")

</div>

Do you have a ‘Case Manager’? This error is occuring because it is  
trying to load your object, and it is empty - a separate issue. Check  
your development.log and see what is trying to load…

- Nic.

On 2/21/06, Craig W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 28:  
> Of course, if I have an entry in ‘first\_name’ the new record is saved
> 
> > > > > def create
> > > > > 
> > > > > This is making me crazy and is a continuation of my issue from last  
> > > > > night which a decent night sleep and breakfast has failed to clarify.
> > > > > 
> > > > > Help!
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–

- Nic

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 21, 2006, 9:59pm UTC](https://www.ruby-forum.com/t/validations-continued/71534/6 "2006-02-21T21:59:15Z")

</div>

Yep, you need to reload your @case\_manager options in your action.  
Instance variables aren’t saved across requests and need to be  
repopulated. And as I stated in the other thread about this, render  
just renders the action, it doesn’t actually call the action method.

So again, in your controller, before you call the render method to  
redisplay the form you have to load the @case\_manager variable with  
something.

Try it. See if it works.  
-Nick

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 21, 2006, 9:59pm UTC](https://www.ruby-forum.com/t/validations-continued/71534/7 "2006-02-21T21:59:16Z")

</div>

I got it…I applied the answer you gave to Seth…

Thanks

Craig

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 21, 2006, 10:08pm UTC](https://www.ruby-forum.com/t/validations-continued/71534/8 "2006-02-21T22:08:36Z")

</div>

Yes indeed - I thought that it would get that information from the ‘new’  
method but it doesn’t so I had to explicitly put it there as you  
suggested. Painful lesson.

Thanks

Craig

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 21, 2006, 9:49pm UTC](https://www.ruby-forum.com/t/validations-continued/71534/9 "2006-02-21T21:49:53Z")

</div>

On Tue, 2006-02-21 at 15:21 -0500, Nick S. wrote:

> Whats the stacktrace? If it says the error is in your view code, then  
> thats where it is, and not in your controller.

* * *

NoMethodError in Clients#create  
Showing app/views/clients/\_form.rhtml where line #25 raised:

You have a nil object when you didn’t expect it!  
You might have expected an instance of Array.  
The error occured while evaluating nil.collect

Extracted source (around line #25):

22: \<%= text\_field ‘client’, ‘gov\_id’ %\>

  
23:  
24: 

Case  
Manager  
  
25: \<%= options = [[‘Select a Case Manager’, ‘’]] +  
@case\_manager.collect {  
26: |cm| [cm.wholename, cm.id] }  
27: select ‘client’, ‘case\_manager\_id’, options %\>

  
28:

As you can see from further down the page…I do have a  
‘case\_manager\_id’…

Request  
Parameters: {“commit”=\>“Create”, “client”=\>{“gov\_id”=\>“111”, “dob  
(1i)”=\>“1920”, “dob(2i)”=\>“2”, “dob(3i)”=\>“21”, “sex”=\>“M”,  
“case\_manager\_id”=\>“13”, “middle\_initial”=\>"", “first\_name”=\>"",  
“last\_name”=\>“test”}}

## Of course, if I have an entry in ‘first\_name’ the new record is saved and all is happy - that’s why I expected validations to capture it.
