# File downloads and REST

**URL:** <https://www.ruby-forum.com/t/file-downloads-and-rest/106973>\
**Category:** Rails\
**Created:** [June 15, 2007, 11:34pm UTC](https://www.ruby-forum.com/t/file-downloads-and-rest/106973 "2007-06-15T23:34:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![g0nzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3bc359/32.png) [@g0nzo](https://www.ruby-forum.com/u/g0nzo)\
**Post date:** [June 15, 2007, 11:34pm UTC](https://www.ruby-forum.com/t/file-downloads-and-rest/106973/1 "2007-06-15T23:34:48Z")

</div>

Hi,

How would an action that sends a file (build dynamically using data from  
model) to user work in REST application? Is it ok if I add “download”  
action to my controller and routes.rb (:collection =\> {:download =\>  
:get})? Or should it be done differently?

---

<div class="post-metadata">

**Author:** ![g0nzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3bc359/32.png) [@g0nzo](https://www.ruby-forum.com/u/g0nzo)\
**Post date:** [June 16, 2007, 12:08am UTC](https://www.ruby-forum.com/t/file-downloads-and-rest/106973/2 "2007-06-16T00:08:18Z")

</div>

I cannot find anything in my resulting tree made with “rails something”.  
I apologize if I’m missing something obvious.

## xc

The only sustainable organizing methods focus not on scale,  
but on good design of the functional unit,  
not on winning battles, but on preservation.

---

<div class="post-metadata">

**Author:** ![g0nzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3bc359/32.png) [@g0nzo](https://www.ruby-forum.com/u/g0nzo)\
**Post date:** [June 16, 2007, 12:41am UTC](https://www.ruby-forum.com/t/file-downloads-and-rest/106973/3 "2007-06-16T00:41:23Z")

</div>

On 6/15/07, Szymon N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi,
> 
> How would an action that sends a file (build dynamically using data from  
> model) to user work in REST application? Is it ok if I add “download”  
> action to my controller and routes.rb (:collection =\> {:download =\>  
> :get})? Or should it be done differently?

If you’re talking about resources, it makes more sense to make that  
file it’s own resource.

map.resources :foos do |foo|  
foo.resource :download # assuming there’s one ‘dynamic’ file per Foo  
record  
end

GET /foos/1/download # downloads/index action.

Part of the rails restful routing style was to stick to the core 7  
actions (index, show, new, edit, update, create, destroy) by bringing  
out the “inner resource”. This makes more sense if folks can also  
update or destroy the extra resource. Then you wouldn’t need several  
actions like show\_download, create\_download, etc.

–  
Rick O.  
[http://lighthouseapp.com](http://lighthouseapp.com)  
[http://weblog.techno-weenie.net](http://weblog.techno-weenie.net)  
[http://mephistoblog.com](http://mephistoblog.com)

---

<div class="post-metadata">

**Author:** ![g0nzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3bc359/32.png) [@g0nzo](https://www.ruby-forum.com/u/g0nzo)\
**Post date:** [June 16, 2007, 1:48pm UTC](https://www.ruby-forum.com/t/file-downloads-and-rest/106973/4 "2007-06-16T13:48:32Z")

</div>

Thanks.

Actually I don’t need to create a file for every resource item  
(:foo),but thanks anyway - maybe I’ll add such functionality, now when I  
know how to do it 🙂

I’d like to be able to access results of my resources index action  
([http://localhost/foos](http://localhost/foos)) in 3 ways - html format - by viewing it in a  
browser, plain text - by getting data using GET “Accept: text/plain”  
(works fine) and a plain text file, which would contain exactly same  
results as previous option, but could be downloaded from the index page  
by clicking a button.

How to do (correctly) something like that?

---

<div class="post-metadata">

**Author:** ![g0nzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3bc359/32.png) [@g0nzo](https://www.ruby-forum.com/u/g0nzo)\
**Post date:** [June 16, 2007, 1:32am UTC](https://www.ruby-forum.com/t/file-downloads-and-rest/106973/5 "2007-06-16T01:32:28Z")

</div>

Hi, could you provide more info about your environment and the  
webserver you’re trying to use?  
Rails version? Webserver?

Thanks,

-Conrad

---

<div class="post-metadata">

**Author:** ![g0nzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3bc359/32.png) [@g0nzo](https://www.ruby-forum.com/u/g0nzo)\
**Post date:** [June 16, 2007, 6:13pm UTC](https://www.ruby-forum.com/t/file-downloads-and-rest/106973/6 "2007-06-16T18:13:04Z")

</div>

Conrad T. wrote:

> Hi, could you provide more info about your environment and the  
> webserver you’re trying to use?  
> Rails version? Webserver?

Sure. Thanks. I’m on Ubuntu Gutsy (like unstable in Debian perhaps)  
recently installed:

root@radioflyer:~/chapter-1# rails -v  
Rails 1.2.3

I have this code in chapter-1/scripts/server:

root@radioflyer:~/chapter-1# cat script/server;echo  
#!/usr/bin/env ruby  
require File.dirname( **FILE** ) + ‘/…/config/boot’  
require ‘commands/server’  
root@radioflyer:~/chapter-1#  
–snip–  
which differs from what I see on page 7 of _Up\_and\_Running_. Presumably  
things have been radically refactored since that publishing.

> > ## xc
> > 
> > The only sustainable organizing methods focus not on scale,  
> > but on good design of the functional unit,  
> > not on winning battles, but on preservation.
> 
> >

–  
The only sustainable organizing methods focus not on scale,  
but on good design of the functional unit,  
not on winning battles, but on preservation.
