开发者

Rails creating local xml file

开发者 https://www.devze.com 2023-01-13 14:54 出处:网络
I need to create a local xml file from a rails application and then copy it to开发者_运维百科 a location on another server.

I need to create a local xml file from a rails application and then copy it to开发者_运维百科 a location on another server.

I have tried using the File.new option to create a new file but it gives me an error saying the file does not exist. After looking closer at the documentation it says that File.new opens a file that already exists.

I can't see any way to create a local file using Ruby, what am I missing?


Assuming you have built up your XML into a string, xml_string, you can do:

xml_file = open(filename, 'w')
xml_file.write xml_string
xml_file.close

Or using the block syntax to achieve this in one line:

File.open(local_filename, 'w') { |f| f.write(xml_string) }
0

精彩评论

暂无评论...
验证码 换一张
取 消