tom*_*ace 8 ruby xml ruby-on-rails builder
我有一个构建器,在调用create时呈现xml.如何跳过渲染步骤,但将xml保存到文件系统?
def create
@server = Server.new(params[:server])
respond_to do |format|
if @server.save
flash[:notice] = "Successfully created server."
format.xml
else
render :action => 'new'
end
end
end
Run Code Online (Sandbox Code Playgroud)
Dan*_*ski 27
XML构建器可以将其数据写入支持<<
运算符的任何对象.在你的情况下String
,File
对象似乎是最有趣的.
使用字符串看起来像这样:
xml = Builder::XmlMarkup.new # Uses the default string target
# TODO: Add your tags
xml_data = xml.target! # Returns the implictly created string target object
file = File.new("my_xml_data_file.xml", "wb")
file.write(xml_data)
file.close
Run Code Online (Sandbox Code Playgroud)
但由于File
该类也支持<<
运算符,因此您可以将数据直接写入文件:
file = File.new("my_xml_data_file.xml", "wb")
xml = Builder::XmlMarkup.new target: file
# TODO: Add your tags
file.close
Run Code Online (Sandbox Code Playgroud)
有关详细信息,请查看XmlMarkup的文档.
归档时间: |
|
查看次数: |
9230 次 |
最近记录: |