This means that you'll have to take this into account when using the string that you get from the form where you are using the datepicker. You have two options, change the way the dates are displayed, by altering all the languages javascripts, or parse the string as it gets to the controller.
The latter can be achieved with this piece of code:
todo.due_date = DateTime.strptime(params[:date],"%d/%m/%Y").to_time
Note that I've chosen that format for the string, but it can be any format according to the ruby's Time class.
There is one other problem though, the user can, maliciously or by distraction, insert an invalid date. We can strengthen our code to prevent this, by catching the exception thrown.
begin
todo.due_date = DateTime.strptime(params[:date],"%d/%m/%Y").to_time
rescue ArgumentError
flash[:error] = t("flash.invalid_date")
redirect_to somewhere_in_the_app_path
return
end
So, if the exception occurs, we set the flash error message (using the translation helper), redirect to the appropriate path, and then return, so that it does not complain of having multiple render or redirect calls.
No comments:
Post a Comment