Matt.C.Wagner@wellsfargo.com
2006-Jan-11 16:02 UTC
[Rails] Output non-HTML (CSV) from Rails
Hi, I want to offer a CSV download of data in my Rails app (eg. An "Export to Excel" link to grab the current view of tabulated data). However, I can''t seem to break-out of the Rails framework to output this non-HTML data. Even if I eliminate my layout template from my view, the most basic view (no HTML) will still wrap the output in <html><body></body></html> tags. Does anyone have a recommended way to do this within Rails? Thank you! Matt -- Matt C. Wagner Information Security Analyst Network Intrusion Detection Security Operations Center Corporate Information Security Wells Fargo Bank
you need to change the Content-Type that you send to the browser to be of type CSV. Something like Content-type: application/vnd.ms-excel http://railswiki.pdxruby.org/HowtoChangeTheContentType.html -adam On 1/11/06, Matt.C.Wagner@wellsfargo.com <Matt.C.Wagner@wellsfargo.com> wrote:> Hi, > > I want to offer a CSV download of data in my Rails app (eg. An "Export > to Excel" link to grab the current view of tabulated data). > > However, I can''t seem to break-out of the Rails framework to output this > non-HTML data. Even if I eliminate my layout template from my view, the > most basic view (no HTML) will still wrap the output in > <html><body></body></html> tags. > > Does anyone have a recommended way to do this within Rails? > > Thank you! > > Matt > > -- > Matt C. Wagner > Information Security Analyst > > Network Intrusion Detection > Security Operations Center > Corporate Information Security > Wells Fargo Bank > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Matt.C.Wagner@wellsfargo.com wrote:> Hi, > > I want to offer a CSV download of data in my Rails app (eg. An "Export > to Excel" link to grab the current view of tabulated data). > > However, I can''t seem to break-out of the Rails framework to output this > non-HTML data. Even if I eliminate my layout template from my view, the > most basic view (no HTML) will still wrap the output in > <html><body></body></html> tags. > > Does anyone have a recommended way to do this within Rails? >In the controller: response.headers[''Content-Disposition''] => ''attachment'' response.headers[''Content-Type''] => ''text/csv'' render :text => @csv_contents, :layout => false -- Alex
Matt.C.Wagner@wellsfargo.com
2006-Jan-11 16:25 UTC
[Rails] Output non-HTML (CSV) from Rails
Adam Denenberg wrote:> you need to change the Content-Type that you send to the browser to be > of type CSV. Something like Content-type: application/vnd.ms-excel > > http://railswiki.pdxruby.org/HowtoChangeTheContentType.htmlExcellent. Thanks Adam! -- Matt C. Wagner Information Security Analyst Network Intrusion Detection Security Operations Center Corporate Information Security Wells Fargo Bank
Hi Matt, Have you tried send_data or send_file which are " Methods for sending files and streams to the browser instead of rendering." I have used send_data to send and image to the browser. Check out my howto ... http://wiki.rubyonrails.com/rails/pages/HowtoSecureFormsWithNoisyImages -bakki kudva On 1/11/06, Matt.C.Wagner@wellsfargo.com <Matt.C.Wagner@wellsfargo.com> wrote:> > Hi, > > I want to offer a CSV download of data in my Rails app (eg. An "Export > to Excel" link to grab the current view of tabulated data). > > However, I can''t seem to break-out of the Rails framework to output this > non-HTML data. Even if I eliminate my layout template from my view, the > most basic view (no HTML) will still wrap the output in > <html><body></body></html> tags. > > Does anyone have a recommended way to do this within Rails? > > Thank you! > > Matt > > -- > Matt C. Wagner > Information Security Analyst > > Network Intrusion Detection > Security Operations Center > Corporate Information Security > Wells Fargo Bank > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060111/0165b3b2/attachment-0001.html
Matt.C.Wagner@wellsfargo.com
2006-Jan-11 20:35 UTC
[Rails] Output non-HTML (CSV) from Rails
Bakki Kudva wrote:> Have you tried send_data or send_file which are " Methods for sending > files and streams to the browser instead of rendering." I have used > send_data to send and image to the browser. Check out my howto ... > >http://wiki.rubyonrails.com/rails/pages/HowtoSecureFormsWithNoisyImages Bakki, Very nice! This seems to be the ideal way to solve the problem. For some reason when rendering through the view (as Alex Young suggested) I was getting ~6500 whitespace lines preceeding the start of my CSV data. But using ''send_data'' in the controller seems to have solved that. And I also just discovered how to direct the filename of the downloadable attachment file with an addition to the Content-Disposition header: send_data csv_data, :type => ''text/csv'', :disposition => ''attachment; filename=output.csv'' Thank you Bakki, Alex, and Adam! Matt -- Matt C. Wagner Information Security Analyst Network Intrusion Detection Security Operations Center Corporate Information Security Wells Fargo Bank
Mr Wagner: On Jan 11, 2006, at 8:02 AM, <Matt.C.Wagner@wellsfargo.com> <Matt.C.Wagner@wellsfargo.com> wrote:> Does anyone have a recommended way to do this within Rails?render_without_layout ''template'' which defaults to the same name as your action. Cheers, Hasan Diwan <hasan.diwan@gmail.com> -------------- next part -------------- A non-text attachment was scrubbed... Name: PGP.sig Type: application/pgp-signature Size: 186 bytes Desc: This is a digitally signed message part Url : http://wrath.rubyonrails.org/pipermail/rails/attachments/20060111/1d4bef4b/PGP.bin