saving file after passing parameter

邮差的信 提交于 2019-12-31 04:02:09

问题


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


回答1:


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/).




回答2:


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



来源:https://stackoverflow.com/questions/8749142/saving-file-after-passing-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!