1

My application uses dates a lot. A lot of ajax calls and urls involve datetimes and I find the typical format '1920-10-10 18:30:00' to be unfriendly for these purposes. I work around this by creating a helper method that basically strips the unnecessary characters out of the date (192010101830) and another method for converting the string back in to a date object.

When I build a url it goes something like this:

=link_to "Send Date", thing_my_date_path(date_to_string(DateTime.now))

Then when the thing_date action receives, it converts the parameter back in to a datetime object

def my_date
  @date = string_to_date(params[:mydate])
  ....
end

This works fine in development. However I am completely open to other suggestions.

The problem is when I go to test my application. Tests fail because the helper methods for date_to_string and string_to_date are not present. I could include them in the tests but I feel like they should be kept separate.

So I'm looking for
a) a better way to pass dates around, and more importantly

b) a method of testing an action that is dependent on helper methods.

1 Answer 1

3

There are built-in methods for that methinks.

> DateTime.now.to_s(:number)
=> "20110429162748" 
> DateTime.parse("20110429162748")
=> Fri, 29 Apr 2011 16:27:48 +0000 

Hope that helps..

Sign up to request clarification or add additional context in comments.

1 Comment

This definitely helps my current situation, but I'm still having difficulty separating the helper methods from the controller tests.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.