Forum: Ruby on Rails Send some text as a csv file?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
73c04e9ef9ca435c5b19a2e765ae6d20?d=identicon&s=25 Max Williams (max-williams)
on 2009-04-29 11:57
I have a table on a page, and i want a link to download the table data
as a csv.  One way to do this would be to have my model class build a
file, and pass the filename back to the controller, which uses send_file
to send it to the user.  But, this seems a bit heavy handed - it's a
small amount of data, which is subject to change, and i don't want a
load of files hanging around if i can help it.

Is there a simple way to just generate some text and push it back as a
file?  eg

1) use a simple view which is rendered normally in the controller, and
change the link_to tag to download the results instead of render it?
(if so, how do i set the filename?)

2) have a normal link_to tag and have the controller build the text
itself (it's just going to be an array join effectively) and push the
text back as a file.

Any advice welcome - thanks!
max
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-04-29 12:12
(Received via mailing list)
On Apr 29, 10:57 am, Max Williams <rails-mailing-l...@andreas-s.net>
wrote:
> I have a table on a page, and i want a link to download the table data
> as a csv.  One way to do this would be to have my model class build a
> file, and pass the filename back to the controller, which uses send_file
> to send it to the user.  But, this seems a bit heavy handed - it's a
> small amount of data, which is subject to change, and i don't want a
> load of files hanging around if i can help it.
>
Have a look at send_data.

Fred
73c04e9ef9ca435c5b19a2e765ae6d20?d=identicon&s=25 Max Williams (max-williams)
on 2009-04-29 12:28
Perfect, thanks (again!) Fred.  I should have worked that out myself
really...

max
This topic is locked and can not be replied to.