How to change date and time in xml response using Rails3?

I am using Rails 3.0.4, Ruby 1.9.2p0 on a Mac. When using Sqlite as the default database and the successfully added response_to: xml function, I see that the datetime format in xml is something like:

<updated-at type="datetime">2011-03-20T12:15:47Z</updated-at>

Although there are several messages on the Internet (also here) in which you can set the date / time format, for example, using the configuration in config / locale / en.yml or add

Time::DATE_FORMATS[:default] = "%Y-%m-%d %H:%M:%S"

in the initialization file do not work.

I think the problem is that when I call someobj.to_xml, for datetime objects, the rails will call

datetime.xmlschema

instead of calling

datetime.to_s

which bypass the settings mentioned above. Although I know that this can be a criminal, I do not know how to fix it. Who has experience? Many thanks!

+3
2

, xmlschema to_s

class DateTime
  def xmlschema
    strftime("%Y-%m-%d %H:%M:%S")
  end
end

, , XLM, , , .

+1

time_formats.rb ,

class ActiveSupport::TimeWithZone
  alias_method :original_xmlschema , :xmlschema
  def xmlschema(fraction_digits = 3)
    original_xmlschema(fraction_digits)
  end
end

Gist on GitHub

+1

All Articles