R
R
Robert2015-05-06 18:42:30
Ruby on Rails
Robert, 2015-05-06 18:42:30

Is it wise to store download files in the public directory?

There is an admin panel functionality that implements the download of a file with certain information.
The model method that implements this looks something like this:

class MyModel
....

def create_and_send_file
   path = "/public/files/info.txt"
  File.open(path, "w+") do |f|
     MyModel.all.each do |obj|
         f.write(obj.name)
    end
  end
    send_file '/public/file/info.txt'
....
end

Is my logic correct?
Or is it generally better to create a separate controller with some action like download_file for these actions? But the question of storage in the public directory remains open.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
vsuhachev, 2015-05-06
@Jowan

You didn't write what you want. In general, it is more reasonable to move the file creation itself out of the controller somewhere, so that it is convenient to test and run from background tasks.
When uploading a file, you do not need to save it to the public folder, and it is also advisable to delete it after uploading so that it does not take up space. Usually Tempfile
is used for this. How to organize the return itself, with a separate controller or method - depends on the circumstances. If you need to give it somehow cunningly with caching or some other logic, it's better to put it in a separate controller. Otherwise, a method.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question