user2719805
user2719805

Reputation:

Create a Text File From a Form Server Side Ruby on Rails

I am using Ruby on Rails and have a form that gets information from user input. I then want to take the user input and write it to a text file on the server side. I hope to save the file in somewhere such as /public/UserInput.txt.

Is there a way to use Ruby on Rails to do this? Or do I need a different language to do this such as PHP? In either case can anyone give me an example of how this is to be done?

Thanks in advance.

Update The code I am trying that is not giving me a text file is:

after_save :create_file 

def create_file 
parameter_file = File.new('C:\\parameter_file.txt', "w")   
parameter_file.puts(:parameter) 
end 

Upvotes: 14

Views: 24150

Answers (1)

sandymatt
sandymatt

Reputation: 5612

This isn't really a rails specific problem. It can be tackled in plain ruby.

path = "/some/file/path.txt"
content = "data from the form"
File.open(path, "w+") do |f|
  f.write(content)
end

where target is where you want the file to go, and content is whatever data you're extracting from the form.

Upvotes: 26

Related Questions