Validate_presence_of strange behaviour

Folks,

I need some help to understand some strange behaviour when using validates_presence_of

I have a form that updates table row. One field is text and I require it to be completed.

With no validation in place in the model I can obvioulsy update the row and either put a text entry in or leave it blank and the update happens okay.

After adding validate_presence_of :activity to the model .... I then update with the text item completed and it updates okay. but if I then update with the text item null I get what appears to be a totally unrelated error:-

"Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id" and which points to the code line:-

<form action="../update/<%= @activities.id %>" method="POST">

Of course what I would expect is to get an "Activity must be completed" error.

So when I would expect validate_presence_of to trigger a real validation error it in fact throws up this other condition.

I suspect the actual error may be unrelated to validate_presence_of but that the validate routine is merely exposing some other underlying issue and without the validation in place it all works okay and I dont have any issues or errors shown!

Confused since the validate_presence_of should be so simple and yet it throws up this other condition.

Can anyone give me a steer as to what/where I should be investigating this.

Any help much appreciated!

You get an error in this line

<form action="../update/<%= @activities.id %>" method="POST">

That has nothing to do with the validation as such. You try to get @activities.id which will fail with the error you mentioned if it's nil So the problem is wherever you do @activities = ...

Why this doesn't fail without the validation is another question

By the way: You use activities here, which is plural and should contain a list rather then an object. For easy code reading it would be better to be clear about that point and either use @activity if it's a singular object or your code is wrong to try to get an id from it (But can't say that for sure without the rest of your code)