In Rails, how can I submit a form and request a csv formatted page?
Because you want to download a CSV, the request should be a GET
request.
Your link should look like <a download href="/reports/1.csv?from=2017-01-01&to=2017-12-31">Download</a>
. I suggest you build this url on browsers (which means using javascript, not rails).
Your rails application must understand the extension .csv
. This can be configured in config/initializers/mime_types.rb
Mime::Type.register "text/csv", :csv
Now you can send CSV to the browser
class ReportsController < ApplicationController
def show
respond_to do |format|
# ...
format.csv do
@csv = ...
send_data @csv.to_s # what you send must be a string
end
end
end
end