Answer the question
In order to leave comments, you need to log in
RoR: How to implement temporary storage of a file in the cache?
Hello, I am new to RoR.
The task I'm trying to solve looks like this:
1. The user uploads an xml file
2. On the application side, I determine how many elements are in this file and prompt the user to select the element to display if there is more than one
3. After selecting the element, if there were several data is written to the database and a report is generated based on them.
How I would like to implement this:
Static_pages#home -> elmts#choose_el -> Report if there are several elements
Static_pages#home -> elmts#choose_el (but without opening the page) -> Report if there is only one element
My question is, how to organize the transfer of a file from the elmts controller to the controller with the Report model?
Maybe there is some kind of temporary storage (cache) where you can put the file and get the id and transfer it between controllers?
Addition:
Or, since xml files compress well, and the size of the uploaded file is unlikely to exceed 500 KB - compress to zip, transfer to view and from there as a parameter to the next controller?
Answer the question
In order to leave comments, you need to log in
Why do you need to store the file? store data in cache
key = UUID.generate.to_s
Rails.cache.write(key, тут xml)
data = Rails.cache.read(params[:key])
How do you applaud the file? If paperclip / carrierwave, then you already have it on the disk
Definitely create an XMLFile model. Take some paperclip. Receive file, save to disk. Handle with the necessary logic. Keep all the information (about the file, about the user, etc., never superfluous). Delete at least manually (everything can be stored in public), at least by the crown, at least by the rake task.
PS Perhaps XMLFile is redundant. But some kind of model that handles the logic is needed. Everything depends on the specific case. If you don't want a model file, make a StaticPageHandler model etc
PSS Of course, you can organize a temporary cache using Rails tools (gem dalli, memcached, or built-in storage, or disk ... ( guides.rubyonrails.org/caching_with_rails.html) ). And you don't need to pass anything to the view. Just save the cache by the user's session key
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question