Saving a file after passing a parameter

Here is the parent question: save the string to a file I want to pass a parameter that will be saved in a file (.csv) after clicking the button.

@bigtable - a table with rows 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 my code does not work: / I want to save @bigtable lines to a file. Each row entry in a table represents a new row in a file. And I want to save the file without redirecting the current page anywhere, but I donโ€™t completely know why :( Please help.


Okay, I know why this does not work - I will add a new route to initialize the savefile method, but how to do it without redirecting / updating the current page with the results? Help plz

+1
string file-io ruby-on-rails-3 save
source share
2 answers

I found a solution - do not write a double post here - link to the topic with the answer: saving the variable to a file and loading it

0
source share

Use <%= form_tag(url, :remote => true) do %> to make a call using Ajax, so your page will not be redirected. Use your serverโ€™s logs to see if the request is being executed (if you want to get the result of ajax call on your page, see http://www.alfajango.com/blog/rails-3-remote-links-and-forms/ ).

+1
source share

All Articles