传递参数后保存文件
这是父问题:将字符串保存到文件 我想传递单击按钮后将保存在文件(.csv)中的参数。
@bigtable 是一个每行都有字符串的表。 这是我的 show.html.erb 中的代码:
...some code here...
<%= form_tag do %>
<% text_field_tag, id = "bigtable", value = @bigtable.to_s %>
<%= submit_tag 'Zapisz' %>
<% end %>
和我的控制器方法:
def savefile
@bigtable = param[:bigtable]
@bigtable.join("\n")
File.open("path/to/file", "w") { |file| file.write @bigtable.join("\n") }
end
但我的代码不起作用:/ 我想将 @bigtable 字符串保存到文件中。表的每一行记录都是文件的一个新行。我想保存文件而不将当前页面重定向到任何地方,但完全不知道为什么:(请帮忙。
好吧,我知道为什么它不起作用 - 我应该添加一些新的路由来初始化 savefile
方法 - 但如何在不重定向/刷新当前页面的情况下进行操作呢?
Here is the parent question: save string to file
I want to pass the parameter which will be saved in file(.csv) after clicking button.
@bigtable is a table with strings in each row.
Here is the code in my show.html.erb:
...some code here...
<%= form_tag do %>
<% text_field_tag, id = "bigtable", value = @bigtable.to_s %>
<%= submit_tag 'Zapisz' %>
<% end %>
and my controller method:
def savefile
@bigtable = param[:bigtable]
@bigtable.join("\n")
File.open("path/to/file", "w") { |file| file.write @bigtable.join("\n") }
end
But mine code doesn't work :/
I want to save @bigtable strings to file. Each row record of the table is a new line of the file. And I want to save file without redirecting current page anywhere but completely don't know why:( Please help.
okay, I know why it doesn't work - I shoud add some new route to initialize savefile
method - but how do it without redirecting/refreshing current page with results? plz help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
<%= form_tag(url, :remote => true) do %>
通过 Ajax 进行调用,这样您的页面就不会被重定向。使用您的服务器日志查看请求是否被执行(如果您想在页面中获取ajax调用的结果,请查看http://www.alfajango.com/blog/rails-3-remote-links-and-forms/)。Use
<%= form_tag(url, :remote => true) do %>
to make the call with Ajax, so your page will not be redirected. Use your server logs to see if the request is executed (if you want to get the result of the ajax call in your page, look at http://www.alfajango.com/blog/rails-3-remote-links-and-forms/).我找到了一个解决方案 - 不要在此处写重复帖子,这是带有答案的主题的链接: 将变量保存到文件并下载
I've found a solution - to not write double post here is the link to the topic with the answer: saving variable to file and downloading it