# Output non-HTML (CSV) from Rails

**URL:** <https://www.ruby-forum.com/t/output-non-html-csv-from-rails/95031>\
**Category:** Rails\
**Created:** [January 11, 2006, 5:04pm UTC](https://www.ruby-forum.com/t/output-non-html-csv-from-rails/95031 "2006-01-11T17:04:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 11, 2006, 5:04pm UTC](https://www.ruby-forum.com/t/output-non-html-csv-from-rails/95031/1 "2006-01-11T17:04:35Z")

</div>

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

 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

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 11, 2006, 5:14pm UTC](https://www.ruby-forum.com/t/output-non-html-csv-from-rails/95031/2 "2006-01-11T17:14:58Z")

</div>

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](http://railswiki.pdxruby.org/HowtoChangeTheContentType.html)

-adam

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 11, 2006, 5:27pm UTC](https://www.ruby-forum.com/t/output-non-html-csv-from-rails/95031/3 "2006-01-11T17:27:02Z")

</div>

removed\_email\_address@domain.invalid wrote:

> 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

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 11, 2006, 9:02pm UTC](https://www.ruby-forum.com/t/output-non-html-csv-from-rails/95031/4 "2006-01-11T21:02:23Z")

</div>

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](http://wiki.rubyonrails.com/rails/pages/HowtoSecureFormsWithNoisyImages)

-bakki kudva

On 1/11/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 11, 2006, 11:31pm UTC](https://www.ruby-forum.com/t/output-non-html-csv-from-rails/95031/5 "2006-01-11T23:31:48Z")

</div>

Mr Wagner:  
On Jan 11, 2006, at 8:02 AM, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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 D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
