Topic: How can I specify content type that I want back from form_tag?

Hi,

when user adds ".csv" to the url in my index action, I'm generating a file with csv data. I'd like to add "download as csv" button to the page as well. Is there a way to specify requested content type in the form_tag, so the index method will trigger respond_to format.csv part?

Thank you in advance

Re: How can I specify content type that I want back from form_tag?

It turns out to be very simple - I just needed to specify "formatted_foos_path(:format)" instead of "foos_path" for form_tag.

Together with the following code:

respond_to do |format|
  format.csv {
    render :text => data.to_csv
    response.headers['Content-Type'] = 'text/csv; charset=utf-8; header=present'
    response.headers['Content-Disposition'] = "attachment; filename=assignments_#{Time.now.strftime("%d-%m-%Y-%H-%M-%S")}.csv"
  }
end

I no longer need separate action for downloading files smile

Last edited by g0nzo (2007-07-16 16:43:03)