In a rails (2.3) app I have been building, I have an XML builder that outputs neat XML. I need to take a snapshot of this XML and store it in the database (or file) upon a certain user action.
How do I get the output of the xml builder view from in the middle of another action?
This code causes a deadlock in a single threaded application...
uri = URI.parse("http://localhost:3000")
response = Net::HTTP.start(uri.host, uri.port) {|http|
http.head('/xmliwanttoarchive.xml')
}
Not sure how开发者_StackOverflow to approach this one.. Cheers.
The above is the wrong way to go about invoking the xml builder.
I overrode the to_xml method instead of using an xml.builder view
def to_xml(options={})
if options[:builder]
build_xml(options[:builder])
else
xml = Builder::XmlMarkup.new
xml.instruct!
build_xml(xml)
end
xml
end
def build_xml(xml)
xml.foo("id" => id, "title" => title) do
xml.group("id" => group.id, "name" => group.name)
xml.bars do
bar.each do |session|
xml.bar("id" => bar.id, "session_time" => bar.important_thing)
end
end
end
Now it can be easily used from within the code and from an action
精彩评论