1

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.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
user3868832
  • 610
  • 2
  • 9
  • 25

2 Answers2

1

I think your problem is that it's evaluating the format.html block, not the format.csv block, because you haven't told it that the required format is csv.

Try changing your first line like so - this uses a named route which is nicer for various reasons:

=simple_form_for :guestlist_booking_search, url: guestlist_bookings_path(format: "csv"), method: :get do |f|
Max Williams
  • 32,435
  • 31
  • 130
  • 197
  • Thanks, but I'm stuck with this error, ActionController::UnknownFormat. I want the form & controller action to be able to render both html and csv using either button. – user3868832 Mar 02 '15 at 17:46
  • Ah yes, sorry, you need to register that mime type (csv) before you can respond with that format of response. This is done in your config. You might be best off just following this, especially if you want do do xls too: http://railscasts.com/episodes/362-exporting-csv-and-excel?view=asciicast – Max Williams Mar 02 '15 at 18:59
1

Ok so the answer I found here

= f.submit "Go"
= f.submit "CSV", value: :csv, name: :format

basically you need to set the format before the form runs.

Community
  • 1
  • 1
user3868832
  • 610
  • 2
  • 9
  • 25
  • That will just pass through `format=csv` in the params, which i think is what my `guestlist_bookings_path(format: "csv")` does. Is the result different then? – Max Williams Mar 02 '15 at 19:00
  • yes but i wanted the same form to be able to do both within the same form block. You put me on the right path though, thank you. – user3868832 Mar 02 '15 at 20:05
  • ah, yes, i just saw the "two submit buttons" bit of your question :) – Max Williams Mar 02 '15 at 20:31