Topic: File downloads and REST design

Hi!

I'm a bit confused by the whole REST oriented app design and I've got a question regarding this.

I've created a custom format that returns results of index action of one of my models in plain text. Now I'd like to add possibility to download results of index action in plain text file as well, by adding somewhere a button on the index page.

I've got everything working by adding additional "download" method to my controller and by modifying my routes.rb file. But I'm not sure if that's the correct approach. I just can't figure out how such thing fits into REST design.

Now it looks like this:

def index
  @requests = Request.find(:all)

  respond_to do |format|
    format.html # index.rhtml
    format.xml  { render :xml => @requests.to_xml }
    format.txt  { render :text => @requests.collect(&:path).join("\r") }
  end
end


def download
  @requests = Request.find(:all)
  data = @requests.collect(&:path).join("\r")
  send_data data, :type => 'text/plain', :filename => 'requests'
end

map.resources :requests, :collection => { :download => :get }

Thanks in advance

Re: File downloads and REST design

First of all I'm new to REST, but afaik a download is a GET. Thus your approach looks absolutely correct to me.

Re: File downloads and REST design

Thanks.

I'm just wondering, if it's ok to actually provide download option of index method as a separate action, or maybe it can somehow be smartly fitted into basic CRUD actions.