Writing string into file and download it using Ruby and jQuery

jQuery, Ruby, Ruby on Rails, 2016-01-25 00:30:01 UTC

1. Action controller for writing string into file and put it in public/docs folder;

File.open(Rails.root.to_s+'/public/docs/filename.xls','w') do |fa|
    fa.puts '<table><tr><td>One</td><td>Two</td></tr></table>'
end

 

2. Calling it using jQuery ajax and this should be downloaded;

$.ajax({url: '/docs/filename.xls'}); 

Share: