将变量保存到文件并下载 [英] saving variable to file and downloading it

查看:172
本文介绍了将变量保存到文件并下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前的问题有问题。没有人不能帮助我,所以我会尝试写这个时候我想要的更好:

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:


  • 我有一个生成器#显示视图,在这个视图中,我想要
    所有的东西,而不需要刷新页面,点击按钮等。

  • 有一个变量(它是无处存储的 - 只是动态放在
    的网站): @bigtable ,这实际上是一个字符串数组

  • 我在这个视图中放置了一个按钮:<%= form_tag do%><%= submit_tag'保存'%><%end%> / code>

  • 如果有人点击该按钮,我想弹出一个窗口,
    他可以设置要保存txt文件的位置(并设置文件名),
    填充所有 @bigtable 数组记录。

  • 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

推荐答案

最后我找到了一个解决方案:

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

这篇关于将变量保存到文件并下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆