Something like the following is written in the Ruby on Rails guides:
Do not store large objects in a session. Instead you should store them in the database and save their id in the session. [..] – http://guides.rubyonrails.org/security.html#session-guidelines
I would like to connect temporary files (e. g. a text file and a 1.2M JPEG picture) with the user session in Ruby on Rails 4.1. The database is no option. I don't want to save files in a blob nor do I want to persist a record to get an ID, because it's a temporary file. I would like to have a clean system, so the temporary files should be cleaned up, after they expire. Similar to the session management of PHP.
Also the other session store types, which Rails offer, dosen't seem to be suitable for this.
Is there already a gem out there, which I can use? Is there a Rails path I don't see, to do something like that? Is that behaviour for some reasons unusual for Rails?
I just recently realized the differences between how PHP stores session content and how Ruby on Rails does that. PHP only stores the session ID at the client side - the content is stored in files on the server, while Rails with it's CookieStore saves everything on the client side. The temporary files of PHP are cleaned up since PHP 5.3 by the garbage collector and it's cron task.
Use memcache or something similar. Create a token and store that in the session, then use that to access the cache.
For a PNG image:
# controller
session[:tmp_token] = SecureRandom.hex(12)
Rails.cache.put(session[:tmp_token], binary_png_data_converted_to_base_64)
Display base64 encoded png
<img src="data:image/png;base64, <%= Rails.cache.fetch(session[:tmp_token]}%>" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With