saving file after passing parameter
Asked Answered
C

2

1

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

Chesterfieldian answered 5/1, 2012 at 20:16 Comment(3)
Could you elaborate on doesn't work? What's actually happening? What doesn't work? Are you sure that savefile is getting called?Teller
just doesn't work - after clicking the button it redirects me to current page again and nothing else - no saving file etc. :/Chesterfieldian
If that's your actual code, you might want to add an action to the form_tag and a route so that you'll actually end up on the savefile action in the controller when clicking the button.Ordure
C
0

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

Chesterfieldian answered 22/1, 2012 at 14:38 Comment(0)
H
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/).

Hipolitohipp answered 22/1, 2012 at 11:13 Comment(0)
C
0

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

Chesterfieldian answered 22/1, 2012 at 14:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.