lundi 16 avril 2018

How to store user input in Rails to a csv file

I gather the user input from several questions in my index.html.erb and store them in a string separated by commas. I want to add their answers to my CSV file and store it in my public directory for my Rails app. How can I do this? Here is what I have so far:

//will print out all answers from form and log them
var exportInfo = "";
document.getElementById('submit').addEventListener('click', function() {
  var newAdjustorInfo = document.getElementsByClassName('adjustorInfo');
  for(var i = 0; i<newAdjustorInfo.length; i++){
  exportInfo = exportInfo + newAdjustorInfo[i].value + ", ";
  writeToFile(exportInfo);


}

//This should write to my file, but it does not
function writeToFile(data){
    var fso = new ActiveXObject("Scripting.FileSystemObject");
    var fh = fso.OpenTextFile("public/update.csv", 8);
    fh.WriteLine(data);
    fh.Close();
}

I can write to my file using erb tags with this code if it helps:

<% require "csv" %>

<% CSV.open("public/update.csv", "ab") do |csv| %>
<% csv << ["did it work?"] %>
  <% csv << ["did it work?"] %>
<% end %>

});

Aucun commentaire:

Enregistrer un commentaire