2

I can export a CSV when I make a request from my localhost to my project folder, but I can't figure out how to export it to my browser downloads folder from the localhost. I am using the send_data method as shown below. Any ideas?

class FitbitApiController < ApplicationController

  before_filter :authenticate_user!

  def data_request
  client = current_user.fitbit_client
  output_filename = ''
  case params[:resource]
  when 'daily_activity_summary'; output = client.activity_time_series(resource: 'calories', start_date: params[:date], period: '1d')
    output_filename = 'daily_activity_summary.csv'
  when 'sleep'; output = client.sleep_logs(params[:date])
    output_filename = 'sleep.csv'
  when 'activities/steps'; output = client.activity_time_series(resource: 'steps', start_date: params[:date], period: '1d')
    output_filename = 'steps.csv'
  when 'weight'; output = client.weight_logs(start_date: params[:date])
    output_filename = 'weight.csv'
end


  # Determine filename based on value of params[:resource]
  filename = params[:resource].split('/').last

  # Write data to CSV
  require 'json_converter'
  converter = JsonConverter.new

  converter.write_to_csv(output, output_filename, headers = true, nil_substitute = '')

  send_data output_filename, :type => 'text/csv; charset=iso-8859-1; header=present', :disposition => "attachment;output=#{output}.csv"

  end
end
Zach S
  • 77
  • 1
  • 8
  • Read my post about import and export CSV: http://nhattan.github.io/rails/csv/2015/07/18/import-export-csv-with-ruby-part-1.html – Tan Nguyen Oct 03 '16 at 10:49

0 Answers0