I can create both xl and csv files formats fine, and would like to create a download link for them within a form.
A user can generate a search for records using this form
=simple_form_for :guestlist_booking_search, controller: 'guestlist_bookings_controller', action: 'index', method: :get do |f|
%fieldset
%legend Guestlist Booking Search
= f.input :lastname
= f.input :start, as: :string, class: "form-control auto_picker1", :input_html => { :class => 'auto_picker1', value: guestlist_booking_search.start.strftime('%d-%m-%Y %H:%M') }
= f.input :finish, as: :string, class: "form-control auto_picker2", :input_html => { :class => 'auto_picker2', value: guestlist_booking_search.finish.strftime('%d-%m-%Y %H:%M') }
= f.submit "Submit"
= f.submit "Download CSV", name: "download_csv"
So the form has two submit buttons, I would like one to process the search and display the results, and the other to process the search and begin downloading an csv file.
So in my index action I have this
def index
if params[:download_csv]
respond_to do |format|
format.html
format.csv { send_data @guestlist_bookings.to_csv }
end
end
end
the guestlist_bookings variable is set in a before block (generating and displaying the search works fine ).
What I can't seem to work out is how to get the file to begin downloading. Currently there is no response from the .xls block. From what I can understand the 'send_data' function is what is used to start a download from the controller.