What is the best way to get an empty temporary directory in Ruby on Rails?
The Dir#tmpdir
function in the Ruby core (not stdlib that you linked to) should be cross-platform.
To use this function you need to require 'tmpdir'
.
ruby Temporary files inside temporary directory
You should use the Tempfile class.
require 'tempfile'
file = Tempfile.new('foo')
file.path # => A unique filename in the OS's temp directory,
# e.g.: "/tmp/foo.24722.0"
# This filename contains 'foo' in its basename.
file.write("hello world")
file.rewind
file.read # => "hello world"
file.close
file.unlink # deletes the temp file
To create temporary folders, you can use Dir.mktmpdir.
How to create temp dir in Ruby?
See documentation for tmpdir. If mktmpdir
method is provided with a block, the temp dir will be removed when block returns. In your case, you would call without a block and handle removal later (=program exit).
Regarding automatic removal on exit, I think tmpdir won't do that for you. However, at_exit should help.
As an example, Homebrew does it like this:
require 'tmpdir'
# rest omitted
TEST_TMPDIR = ENV.fetch("HOMEBREW_TEST_TMPDIR") do |k|
dir = Dir.mktmpdir("homebrew-tests-", ENV["HOMEBREW_TEMP"] || "/tmp")
at_exit { FileUtils.remove_entry(dir) }
ENV[k] = dir
end
Ruby: Could not find a temporary directory
Not sure what happened here, but I believe it had something to do with the /tmp folder permissions. I thought my /tmp folder was corrupted so I looked around about deleteing that folder and restoring it (I wasn't sure if this folder was especially significant about the way it was created). I found this source that suggested you can simply make the /tmp folder, just as you would any other folder, and then do a chmod 1777
on the newly created folder.
So, instead of deleting my current /tmp, I ran this chmod command and everything appeared to work.
What is strange to me is that I had previously done a chmod 777
and that caused the folder to not work. Weird...
Change temporary directory in Rails 4
It seems that some libraries like to hard code values... See: [1]
By adding the following you can get around the hard coded value:
config.assets.cache_limit = 50.megabytes
config.assets.configure do |env|
env.cache = Sprockets::Cache::FileStore.new(
File.join(ENV['RAILS_TMP'], 'cache/assets'),
config.assets.cache_limit,
env.logger
)
end
Where is located the temporary folder when we upload a file
The location of your tempfile is probably system dependent, likely in the /tmp
folder somewhere.
But you dont really need to know.
The uploaded image is either in memory as a StringIO object, or saved to a temporary file on the host server. This is an optimization provided by rails. If the image is small enough, Rails will pass you a StringIO object, which is basically the entire image loaded in memory. If the image is larger, it will pass you an instance that represents a temporary file on disk.
You want to just save it to a known folder, say uploads
. You do not need to care if the image is stored in memory or stored on disk.
Both StringIO objects and temp file backed objects respond to the :read
method. All you have to do is call read to get the data and then write it out to the location you want.
image_as_io = params[:image]
filename = ... # determine a filename for the image
File.open(Rails.root.join('public', 'uploads', filename), 'wb') do |file|
file.write(image_as_io.read)
end
Rails could not find a temporary directory (ArgumentError)
edit: As suggested in the comments just run
chmod +t /tmp
========
Old answer:
Your Ubuntu installation probably doesn't have TMPDIR
set. You should set that variable in your startup.
This link has more information on environment variables in Ubuntu: https://help.ubuntu.com/community/EnvironmentVariables
Related Topics
"Errno::Eaccess...Permission Denied" Running Compass Watch
How to Reference a Lambda from Within Itself Using Ruby
What Is the Best Wysiwyg for Rails - Ruby on Rails Blog
Vi Input Mode in Command Line Matlab
Rails: Find Rows Without Connection in Habtm Relation
Removing Child Root Nodes in Rabl
Gem Not Found in Ruby Cron Job in Rvm Env
Nameerror: Uninitialized Constant Faker
Rubocop Error 'Class Definition Is Too Long Ruby'
Model Using Modules in Rails Application
How to Bypass Mass Assignment Protection
Paperclip Amazon S3 Setup with Heroku
What Is the Community-Preferred Ruby Unit Testing Framework
Aptana 3 Ruby Debugger - Exception in Debugthread Loop: Undefined Method 'Is_Binary_Data'