File creation in Rails

I am passing the contents of a textarea in my rails view to the server
by a POST call. How can I store the contents as a separate file, say
contents_userId.txt on my server and at the same time store my file in
database allowing me to access the various files a single user
has…or for any other purpose.

To access files in rails, you can use the Ruby File Class

http://ruby-doc.org/core/classes/File.html#M002579

You can put this into a method in the model. Then either call it
specifically from the controller, or use an after_update callback to
automatically create the file after the object has been saved to the
database

def save_file(filename)
File.open “#{RAILS_ROOT}/tmp/#{filename}”, ‘w’ do |f|
f.write contents
end
end

Tonypm

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs