开发者

CSV.open and send_data in rails....?

I may end up figuring this out later, but i thought I'd try.

Can someone help combine send_data and CSV.open

According to the docs, you can CSV.open filename, mode(whatever that is) and basically a file will save to your current path. However, if you want to send that file to a user through his broswer, as most of us who give the option of CSV files to download, do,do... then ca开发者_Python百科n we combine the CSV.open with send_data?

thoughts?

examples welcome if you do something like this too.


I don't think you want to combine those two things.

CSV.open will save the data to a file, which you would need to read back in in order to send it through send_data.

But you can do something like:

csv = []
csv << ["titles", "for", "csv"]
csv << ["data", "for", "csv"]
send_data(csv.collect{|s| s.join(",")}.join("\n"), 
          :type => 'text/csv; charset=utf-8; header=present', 
          :filename => "mytitle.csv")

Which should prompt the user to download the csv file.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜