1

I have a javascript file that uses ajax and passes an array of ids to a rails controller action. The controller then maps these to model objects and generates a file containing some of their data. My problem lies in downloading this file.

Before, I was saving the needed objects in the database first, and then the controller could just format as .ics, call the same action, then the file would be downloaded.

Now everything is more dynamic, and I'm having trouble rendering the file now that I have to pass in params. Is there a way to do this? I've tried

render :layout => false, :text => @calendar.to_ical

and

send_data @calendar.to_ical, :type=> 'text/ics'

and

render :text => @calendar.to_ical

All of which complete successfully, but no file is ever downloaded.

Any help would be appreciated! Thank you!

atallest
  • 97
  • 1
  • 13

1 Answers1

1

The short answer is you cannot use AJAX to download files (for security reasons). Check out this question for other options, like setting window.location= or using the jQuery File Download plugin.

Community
  • 1
  • 1
messanjah
  • 8,977
  • 4
  • 27
  • 40
  • Yes! I was able to get it working with setting the window location and passing it the needed array. Used help from this question too. http://stackoverflow.com/questions/15859254/send-array-to-the-controller Thank you!! – atallest Mar 28 '15 at 19:48