I have wrote an validation function for checking that my starting date
should be greater than the current date as like
validate :start_date_should_not_be_greater_than_current_date
def start_date_should_not_be_greater_than_current_date
if self.start_date > Date.today
errors.add("Whatever")
end
end
But the problem is that when i left empty the field for start_date when
filling values in form, this error is coming:-
----
You have a nil object when you didn''t expect it!
The error occurred while evaluating nil.>
----
I have also wrote some exceptions for my methods but these exceptions
are not catching this error.
Can you guys tell me how to tackle with this situation..
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
If the field is empty, then self.start_date is nil. Try this:
def start_date_should_not_be_greater_than_current_date
if !self.start_date.nil?
if self.start_date > Date.today
errors.add("Whatever")
end
end
end
or something similar
On Mar 5, 8:08 am, Hemant Bhargava
<li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>
wrote:> I have wrote an validation function for checking that my starting date
> should be greater than the current date as like
>
> validate :start_date_should_not_be_greater_than_current_date
>
> def start_date_should_not_be_greater_than_current_date
> if self.start_date > Date.today
> errors.add("Whatever")
> end
> end
>
> But the problem is that when i left empty the field for start_date when
> filling values in form, this error is coming:-
> ----
> You have a nil object when you didn''t expect it!
> The error occurred while evaluating nil.>
> ----
>
> I have also wrote some exceptions for my methods but these exceptions
> are not catching this error.
>
> Can you guys tell me how to tackle with this situation..
> --
> Posted viahttp://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
> > validate :start_date_should_not_be_greater_than_current_date >Personally I''d rename that to: valid :start_date_before_now And then I''d do this as the method: def start_date_before_now if start_date && start_date > Date.today errors.add("Whatever") end end Just check start_date first. You don''t need the .nil? check as nil evaluates to false (and any valid date is true). Note, you don''t need to do self.start_date when reading a value, only when assigning to it (the reason is that reading the value will default to calling the method if no local variable exists, but writing a value will default to creating a local variable rather than calling the value= method). Cheers, Andy -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Andy Jeffries wrote:>> >> validate :start_date_should_not_be_greater_than_current_date >> > > Personally I''d rename that to: > > valid :start_date_before_now > > And then I''d do this as the method: > > def start_date_before_now > if start_date && start_date > Date.today > errors.add("Whatever") > end > endThanks andy.. This worked like a charm .. :)> Cheers, > > > Andy-- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.