Search code examples
ruby-on-rails-3buttonmethodsdownloadsaving-data

saving variable to file and downloading it


I've got a problem with my previous question. Nobody can't help me so I'll try to write better this time what I want:

  • I've got a generators#show view, and in this view I want to do everything without refreshing page after clicking a button etc.
  • I've got a variable (it is nowhere stored - just dynamically put on the site): @bigtable, which is actually an array of strings
  • I have a button placed in this view: <%= form_tag do %><%= submit_tag 'Save it' %><% end %>
  • If someone will click on that button I want to pop up an window and he could set where he want to save a txt file (and set filename), filled with all @bigtable array records.

For couple days I think I've tried everything but I don't really understand how to solve it. Could someone, please write here what methods in model and controller I should write? And all required routes? I'll really appreciate any help. My previous thread (but please do not send me there or give same answers like there): saving file after passing parameter


Solution

  • finally I've found a solution:

    def savefile
        @generator = Generator.new(params[:generator])
        @bigtable = Rails.cache.read("pass")
        doc = "wyniki.csv"
        File.open(doc, "w"){ |f| f << @bigtable}
        send_file(doc, :type => 'text; charset=utf-8')
      end