ruby - How to implement a user creating a file via form and then downloading it in rails? -
I am creating an online translation platform. When a job is translated and saved, I want the user to download the translated version as a text file. It is currently saved in a string named "target_text" in a string.
I know in Ruby that I can use this method:
File.open ("translation.txt", 'W') {| F | | F.write ("my translated string")}
I agree that I can face the location to save the file in front of "translation.txt", but I do not I'm sure what folder should be specified within my app?
In addition to this, I want the file to be attached to the "job" object, in the same way that the paperclip file can be attached, the difference is the start of the server side. How should I go about this?
I have tried to find an answer on this, and I have to make sure that I want to do it purely. I appreciate the directions for a better place to understand this concept too.
I do not think quite a question, but I hope it can help ... < / P>
File.open ("translation.txt", 'w') {| F | F.write ("my translated string")}
Try using the following
Tempfile.open (['translation', '. Txt '], Rails.root.join (' tmp ')). File | # This will create a temp file in RAILS_ROOT / tmp / folder #, you can replace any auto-generated text for the # part of the translated text, for example # Tempfile.open ([user.id.to_s, '_translation .txt '] #RAILS_ROOT / tmp / 1_translation.1fe2ed.txt # Make # 1 2 2 2 1 2 2 2 2 2 2 2 2 1 2 2 2 1 Next Make sure that block # will be closed and will delete this template file ... so that the TMP DIR is not bigger. # You can add the file to the paperclip attachment @ user.translation = file # for example that it is Naming that the user has a paperclip attachment, close the translation # and delete the file file, close the file, also check Unlink End End
... this is the practice that I I am not sure ... whether it is best or not .. but no issue has been made yet (even with paperclip S3 storage)
Comments
Post a Comment