Works on intial show, but not after validation error

I have a shipment model with a method called returnable?

If a shipment has been delivered, it figures out what the last
returnable date is, delivery date + 14 days.

In Shipment Model

def returnable?
if last_return_date.nil?
return “Yes”
else
n = Time.now.to_i.abs.round
d = last_return_date.to_i.abs.round
if d >= n
return “Yes”
else
return nil
end
end
end

This works great on the intials edit view, but if the form is submitted
and there is a validation error, it gives the following error message:

NoMethodError in Shipments#update
undefined method `to_i’ for #<Date: 4910233/2,0,2299161>

Just wondering what’s up?

Both the update action and edit action grab the @shipment instance var
the same way, so this should totally work.

If I’m just a noob over complicating the “is this date greater than this
date” thing and there’s a better way to write that, please feel free to
correct me.

Any help is appreciated.

Thanks,

Dave

On the writing this less complicated front, I think I’ve got a better
version:

def returnable?
is_one = last_return_date <=> Time.now
if is_one >= 1
return “Yes”
else
return nil
end
end

Seems when my form submits and there’s a validation error, my fields of
type Time get transformed to type Date, so the to_i wasn’t available as
a method.

no clue why that is…

I’m thinking you’ve not got a valid shipment object when you recover
from
the validation error.
Can you post your controller code?

Regards,
Dave


Information and Educational Technology
Kwantlen University College - 604-599-2120
“So powerful is the light of unity that it can illuminate the whole
earth.” --Bahá'u’lláh

Dave C. [email protected]
Sent by: [email protected]
22-05-2007 03:57 PM
Please respond to
[email protected]

To
[email protected]
cc

Subject
[Rails] Re: works on intial show, but not after validation error

On the writing this less complicated front, I think I’ve got a better
version:

def returnable?
is_one = last_return_date <=> Time.now
if is_one >= 1
return “Yes”
else
return nil
end
end

Seems when my form submits and there’s a validation error, my fields of
type Time get transformed to type Date, so the to_i wasn’t available as
a method.

no clue why that is…


Posted via http://www.ruby-forum.com/.

Hi Dave,

I figured this one out. In my database, the date fields were of type
datetime. However, when I displayed them in the form I was only showing
mm/dd/yyy via strftime.

So what was a Time object got turned into a Date object if a validation
error occurred. Since the datetime field only had the date in it anyway,
I just changed it’s type, changed all my Time.now’s to Date.today’s and
it’s all fixed.

Thanks for your help,

Dave C

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs