Forum: Ruby on Rails validation is giving me heartburn

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Dave D. (Guest)
on 2006-03-10 22:31
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:

<p><label for="build_version">Version</label><br/>
<%= text_field 'build', 'version'  %></p>

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!
Nick S. (Guest)
on 2006-03-10 23:29
(Received via mailing list)
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
Anthony G. (Guest)
on 2006-03-12 20:01
> 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
Anthony G. (Guest)
on 2006-03-12 20:17
> 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
dave davidson (Guest)
on 2006-03-12 22:54
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.
This topic is locked and can not be replied to.