Reputation: 329
I need to export some data into a text file. I have searched a little and I can't seem to find anything useful and recent. I'm already using pdfkit and csv to export data to pdf and csv files, so I don't know if I can use any of these to make the text. Anyone had this problem and found a solution recently?
EDIT
My question is a little vague, I'll specify more. I need to do something like CSV.generate does. Add rows for every value of the attribute in the register. I find the register, I store it in a variable, and then I need to "add" every value as a row in the textfile. The part of make a loop through the attributes of the register found I got it working, what I don't know is how to copy it. That's the part where I'm stuck.
EDIT 2
I ended up passing an array of all the attributes of the register and saving it in a local variable, then I did what you suggested and it works perfectly.
Upvotes: 0
Views: 1883
Reputation: 7723
In order to export/write something inside text field. first you need to open text file with write mode and then need to write data to it.
file = File.open("/home/sandip/myfile.txt", "w")
file.write "this is how text file can be written"
file.close
file = File.open("/home/sandip/myfile.txt")
file.read #=> this is how text file can be written
If you need to export database records to csv file then make use of csv library its pretty straight forward.. click here
Upvotes: 1
Reputation: 467
Quick search: http://railsforum.com/viewtopic.php?id=37888
@content = "Hello World"
send_data @content,
:type => 'text',
:disposition => "attachment; filename=your_file_name.txt"
see also http://api.rubyonrails.org/classes/ActionController/Streaming.html
Upvotes: 1
Reputation: 775
Its similar to other file format - export
Try like this
def download_textfile
@content = get_content
send_data @content,:type => 'text',:disposition => "attachment; filename=file_name.txt"
end
Upvotes: 0