From 1e1d60b463674b0471f65249665e75b1f1e4990d Mon Sep 17 00:00:00 2001 From: Dylan Griffith Date: Wed, 5 Jun 2019 14:45:11 +1000 Subject: [PATCH] Make the CSV download button a link This changes from using a form to using an `` tag. The advantage of using a link is that the URL can easily be copy/pasted and the data can be downloaded programmatically. The reason I wanted to make this change is I wanted to write a small script that parses this CSV data and I cannot do that unless I can download the data programmatically. --- app/assets/javascripts/application/charts.coffee | 9 ++++----- app/controllers/raw_data_controller.rb | 2 +- config/routes.rb | 2 +- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/app/assets/javascripts/application/charts.coffee b/app/assets/javascripts/application/charts.coffee index d0e4909..db1964c 100644 --- a/app/assets/javascripts/application/charts.coffee +++ b/app/assets/javascripts/application/charts.coffee @@ -146,12 +146,11 @@ loadChart = (element, chart_config, query_string, view_string) -> chart_config['data'] = transform(data, view_string) context = element.getContext('2d') download_csv_path = $('#download-csv-path').val() + query_params = {query: query_string, name: view_string} + download_url = "#{download_csv_path}?#{$.param(query_params)}" $(element).parent().prepend( - "
" + - "" + - "" + - "
" + - " (This CSV always includes data within 2 years regardless which charts)" + "
Download CSV" + + " (This CSV always includes data within 2 years regardless which charts)" ) chart = new Chart(context, chart_config) async: true diff --git a/app/controllers/raw_data_controller.rb b/app/controllers/raw_data_controller.rb index 232942f..4f6f468 100644 --- a/app/controllers/raw_data_controller.rb +++ b/app/controllers/raw_data_controller.rb @@ -8,7 +8,7 @@ class RawDataController < ApplicationController def download respond_to do |format| format.csv do - send_data csv_data, filename: "#{params[:name]}.csv" + send_data csv_data end end end diff --git a/config/routes.rb b/config/routes.rb index b00ab13..523c7d7 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -44,7 +44,7 @@ Rails.application.routes.draw do end post "/graphql", to: "graphql#execute", as: "graphql" - post "/download/raw.:format", to: "raw_data#download", as: "download_raw_data" + get "/download/raw.:format", to: "raw_data#download", as: "download_raw_data" # The priority is based upon order of creation: first created -> highest priority. # See how all your routes lay out with "rake routes". -- GitLab