How should look link the json response so that ActiveResource may generate a DateTime object from a date?

StackOverflow https://stackoverflow.com/questions/17861991

I have a question object with a created_at attribute in it. When I will find a question i get back the created_at from the REST service, but Activeresource do not convert the date string to DateTime.

Question.find(1)
/questions/1.json

Question.find(1).created_at.class
=> String

In which format should be the Json response, so that ActiveResource will convert it to a DateTime?

If it is not possible, what should i do?

有帮助吗?

解决方案

According to slide #14 in this presentation, you can do this in config/initializers/active_resource.rb:

ActiveSupport::JSON::Encoding.use_standard_json_time_format = true
ActiveSupport.parse_json_times = true

其他提示

JSON will always be a string as this is how it passes data as stringyfied objects. If you want to convert it to a date/time object you will need to parse it at the server side.

Time.parse should do the trick

http://ruby-doc.org/stdlib-2.0/libdoc/time/rdoc/Time.html#method-c-parse

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top