saving file after passing parameter

2019-02-28 02:41发布

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

2条回答
啃猪蹄的小仙女
2楼-- · 2019-02-28 03:25

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

查看更多
一夜七次
3楼-- · 2019-02-28 03:36

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

查看更多
登录 后发表回答