Hi
I have two date fields in table start_date and end_date .There data
type are datetime. Now in the view I am accepting values of them as
text fields like And I am following RailsCast 32
def start_date_string=(start_date_str)
self.start_date = Time.parse(start_date_str)
end
def end_date_string
end_date.to_s(:db)
end
def end_date_string=(end_date_str)
self.end_date = Time.parse(end_date_str)
end
But my problem is validates_presence_of :start_date is not working
Even if I dont give any values to start date it does not show any error
message. Please help
When you say it is not working, what exactly is not working? Two
things should happen if the validate fails, firstly the record should
not get saved to the database and secondly an error message should be
made available. You have said that you are seeing no message but is
the record getting saved or not?
How are you showing the error message? An error here could be the
explanation for not seeing the message.
Have you tried using ruby-debug to break into your controller and see
what is happening when you submit? If you do not know how to do this
have a look at the rails guide on debugging at
http://guides.rubyonrails.org/.
This is not a similar problem at all. The OP had an issue with
validations, yours is showing an error.
I don't see the problem immediately, but I suggest removing bits of
code till it does not show the error, then putting it back a little at
a time till you find exactly what is causing the problem.
That does not appear to be exactly the same code as shown in the error
above. Above it has a newline after :formdate_string, as it shows the
%> on line 85. Could that be part of the problem I wonder?
Thanks for your prompt reply. It wasn't the new line, but that
difference was well spotted.
I found it... makes sense now... ArgumentError... it wasn't getting
passed an argument for a new form. The form worked for the edit action
when the field was populated.
I needed to initialize the formdate field with a value. So I added
this to the "new" action of the controller.
eg.