# Validation is giving me heartburn

**URL:** <https://www.ruby-forum.com/t/validation-is-giving-me-heartburn/54642>\
**Category:** Rails\
**Created:** [March 10, 2006, 9:31pm UTC](https://www.ruby-forum.com/t/validation-is-giving-me-heartburn/54642 "2006-03-10T21:31:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sp00nyg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@sp00nyg](https://www.ruby-forum.com/u/sp00nyg)\
**Post date:** [March 10, 2006, 9:31pm UTC](https://www.ruby-forum.com/t/validation-is-giving-me-heartburn/54642/1 "2006-03-10T21:31:43Z")

</div>

this one is killing me. all i want to do is validate the format of  
user-entered software versions. so in the model i have:

validates\_format\_of :version,  
:message=\>“Not valid version number.”  
:with=\>/[0-9].[0-9]{2}.[0-9]{2}.[0-9]{2}.[0-9]{2}/

the form field is simply the scaffold form code:

Version  
\<%= text\_field 'build', 'version' %\>

putting in a correctly-formatted number, i.e. 4.11.43.22.55, works and  
the record is saved. however putting junk in the field gives me:

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.inject

Huh? So I go to the console and create a new object:

> > b=Build.new(:product\_id=\>34, :version=\>“this won’t work”, …)  
> > b.save  
> > ==\>false

> > b  
> > #\<Build:0x37a4480 @errors=#\<ActiveRecord::Errors:0x37a2230  
> > errors={“version”=\>[“Must be valid.”]},… [other stuff…]

so obviously if i’m getting the error message, the validation is being  
processed by the model. so why the weird ‘nil object’ error in the  
view? the form is all stock scaffolding, no modifications whatsoever.

btw, i just tried validates\_presence\_of on another field, and i get the  
same exact behaviour. so i _know_ i’m missing something simple here.  
what is it?! 1/2 day supply of pepcid ac for any assistance.

thanks!

---

<div class="post-metadata">

**Author:** ![sp00nyg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@sp00nyg](https://www.ruby-forum.com/u/sp00nyg)\
**Post date:** [March 10, 2006, 10:29pm UTC](https://www.ruby-forum.com/t/validation-is-giving-me-heartburn/54642/2 "2006-03-10T22:29:45Z")

</div>

Paste the whole error message. My first guess it that the problem is  
in your view and not your model.

Do you use any lists to populate drop down boxes? If so be sure to  
reload them before going back to the view.

-Nick

---

<div class="post-metadata">

**Author:** ![sp00nyg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@sp00nyg](https://www.ruby-forum.com/u/sp00nyg)\
**Post date:** [March 12, 2006, 7:01pm UTC](https://www.ruby-forum.com/t/validation-is-giving-me-heartburn/54642/3 "2006-03-12T19:01:02Z")

</div>

> Do you use any lists to populate drop down boxes? If so be sure to  
> reload them before going back to the view.

I’m getting a similar problem to Daves and yes I have drop down boxs.  
But its a very strange error and I was wondering if its a bug in rails.

I was using Rails 1.0 now frozen\_edge.

I submit data from a form to save a new object but the form is empty.  
The validation methods of the class don’t execute instead the nil.inject  
error is occurring.  
If I remove all validation methods from the class then no nil.inject  
error occurs. If the fields that need validating are filled in then no  
nil.inject error occurs.

How do you reload the drop down menus in the view as Nick suggests ?

Tony

---

<div class="post-metadata">

**Author:** ![sp00nyg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@sp00nyg](https://www.ruby-forum.com/u/sp00nyg)\
**Post date:** [March 12, 2006, 7:17pm UTC](https://www.ruby-forum.com/t/validation-is-giving-me-heartburn/54642/4 "2006-03-12T19:17:03Z")

</div>

> How do you reload the drop down menus in the view as Nick suggests ?

I tried adding a instance variable that holds the collection that  
populates the select menu to the create method in the objects controller  
and that fixed it.

So if you have a lookup table you use to populate the options of a  
select menu make sure you add them to the create method as well as the  
new method and the edit method.

Is there a reason why render :action =\> new doesn’t do this ?

Tony

---

<div class="post-metadata">

**Author:** ![sp00nyg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@sp00nyg](https://www.ruby-forum.com/u/sp00nyg)\
**Post date:** [March 12, 2006, 9:54pm UTC](https://www.ruby-forum.com/t/validation-is-giving-me-heartburn/54642/5 "2006-03-12T21:54:25Z")

</div>

Anthony G. wrote:

> > How do you reload the drop down menus in the view as Nick suggests ?
> 
> I tried adding a instance variable that holds the collection that  
> populates the select menu to the create method in the objects controller  
> and that fixed it.
> 
> So if you have a lookup table you use to populate the options of a  
> select menu make sure you add them to the create method as well as the  
> new method and the edit method.

Brilliant… added the collection to the create method and all works  
perfectly now!

> Is there a reason why render :action =\> new doesn’t do this ?

I’m with Tony on this one. Looking at the scaffold code for the create  
method, if the object does not  
save, the ‘new’ action is rendered. So, if the instance variable for  
the drop-down collection is defined in ‘new’, why would rails complain  
about nil?

What specifically is it about adding validations to the model that is  
gumming up the works? I would imagine that if you dont, in this case,  
add the appropriate collection for the drop-down to both the ‘new’ _and_  
‘create’ methods, the nil error would appear regardless of whether  
validation is present. However, this is not the case. In my-- and  
presumably Tony’s-- code, the additional instance variable definition  
was not present in the ‘create’ method yet no error was thrown when a  
record was added to the database. Only after validation was added did  
this behaviour occur.
