Customizing XML generation in Rails -
i new rails , have written few models. 1 of xml generated model looks this:
<book> <read type="boolean">true</read> <title>julius caesar</title> <id type="integer">1</id> </book>
the serialized xml want have more control on it. want generate same in different format. like:
<book read="true" id="1"> <title>julius caesar</title> </book>
how achieve this? have done research , found to_xml method should overridden. not sure how it.
you can use custom ::builder::xmlmarkup
this. however, the documentation active record serialization (see last code example) buggy. can this:
class book < activerecord::base def to_xml(options = {}) # load builder of not loaded yet require 'builder' unless defined? ::builder # set indent 2 spaces options[:indent] ||= 2 # initialize builder xml = options[:builder] ||= ::builder::xmlmarkup.new(:indent => options[:indent]) # real action starts xml.book :read => read, :id => id xml.title self.title end end end
Comments
Post a Comment