Errno::ENOENT: No such file or directory ruby
You can't save to a non-existing path. As the developer you're responsible to make sure your destination paths exist prior to trying to save the file.
Ruby has methods that let you check for the existence a file or directory:
- File.exist?('path/to/file')
- File.file?('path/to/file')
- File.directory?('path/to/file')
- Dir.directory?('path/to/dir')
- Dir.mkdir('path/to/dir')
- FileUtils.makedirs('path/to/dir')
FileUtils.makedirs()
is an alias to mkpath
, and mkdir_p
. I prefer makedirs
because it's a mnemonic.
These will also be useful:
- Dir.chdir('path/to/dir')
- FileUtils.chdir('path/to/dir')
Between those two I prefer Dir.chdir
, because it takes a block. Any actions that occur inside that block happen in the directory you changed to. At exit, the directory reverts to what it was previously.
Errno::ENOENT at / no such file or directory
You need to configure your application instance, something like this should work:
require 'rubygems'
require 'sinatra'
module Registration
class HelloWorldApp < Sinatra::Base
configure do
set :public_folder , File.expand_path('../public', __FILE__)
set :views , File.expand_path('../views', __FILE__)
set :root , File.dirname(__FILE__)
set :show_exceptions, development?
# Optional: Load from external file
#YAML.load_file('path/to/config.yml').each do |k, v|
# set(k.to_sym, v)
#end
end
get '/' do
erb :index
end
end
end
Then:
bundle exec rackup
Errno::ENOENT: No such file or directory @ rb_file_s_mtime after feature tests
The fact that the test passes just means that the test does not depend on that image being found. A broken image link will not generally cause a test to fail and it is in fact often the case that test writers do not even bother to supply mock images because it is (perceived to be) too much work and not relevant to the thing being tested.
See folks, this is why we ask for an example. Now that the OP has posted an example, we can see that his problem is browser caching, and will be solved by replacing
product.images.attach io: File.open(path), filename: 'image.png'
with
product.images.attach io: File.open(path), filename: "image-#{Time.now.strftime("%s%L")}.png"
in the helper that does the attachment. I would not have solved this without having the example.
Explanation
ActiveStorage handles saving and serving files. OP was saving and serving images with it, which is definitely what it was intended for. In order to allow for different services to serve files in different ways, ActiveStorage separates the published URL from the actual image serving URL.
The published URL is almost a permalink: it is an encoded version of the database key for the attachment. ActiveStorage processes a request for the URL by looking up where the file is stored and sending a 302 temporary redirect to the URL that can be used to access the file, called a "service URL". When using AWS S3 to store files, the service URL can be a signed URL that expires quickly but nevertheless connects the browser directly to S3 rather than having to go through the web server as intermediary.
By default, the service URL is good for 5 minutes, and ActiveRecord explicitly sets
Cache-Control: max-age=300, private
on the 302 redirect. The browser caches this redirect response and for the next 5 minutes the browser will not even attempt to use or verify the published URL, it will immediately replace the public URL with the cached service URL.
Unfortunately, while the public URL is predictably recreated, the service URL is randomly generated, so when the browser does its automatic redirection, the previously valid service URL no longer works. The solution (or workaround, depending on your point of view) is to distinguish the public URLs by including a timestamp in the filename, so that tests will not reuse public URLs.
Sidenote
By the way, you should not be using url_for
with image_tag
. Change your ERB from
= image_tag url_for(product.image.variant(resize: "120x120"))
to
= image_tag(product.image.variant(resize: "120x120"))
Rails is smart enough to handle either case, but the latter way is the recommended way.
Ruby: No such file or directory @ rb_sysopen - testfile (Errno::ENOENT)
File.open(..., 'w')
creates a file if it does not exist. Nobody promised it will create a directory tree for it.
Another thing, one should use File#join
to build directory path, rather than dumb string concatenation.
path = File.join Rails.root, 'public', 'system', 'users', user.id.to_s, 'style'
FileUtils.mkdir_p(path) unless File.exist?(path)
File.open(File.join(path, 'img.jpg'), 'wb') do |file|
file.puts f.read
end
Why would I get Errno::ENOENT: No such file or directory when viewing a Sinatra form?
If you cannot get shotgun
to work then the new recommended way to reload Sinatra seems to be rerun
.
To use it:
> gem install rerun
> cd /Users/HelenasMac/Desktop/RubyForm
> rerun ruby basics.rb
Errno::ENOENT No such file or directory Rails 4
Change sass-rails version to 4.0.3 and leave the rest as it is. Remove Gemfile.lock when necessary, and then:
$ bundle update
$ bundle install
Finally, don't forget to restart your rails server!
Related Topics
Rails 3: Generate Unique Codes (Coupons)
How to Reinstall a Gem Using Bundler
Xcode - Configure: Error: No Acceptable C Compiler Found in $Path
Setting Ruby Hash .Default to a List
How to Rescue Model Transaction and Show the User an Error
Testing Actionmailer Multipart Emails(Text and HTML Version) with Rspec
Creating an Empty File in Ruby: "Touch" Equivalent
"Ago" Date/Time Functions in Ruby/Rails
Which Ruby Memoize Pattern Does Activesupport::Memoizable Refer To
How to Check from Ruby Whether a Process with a Certain Pid Is Running
How to Sort a Hash by Value in Descending Order and Output a Hash in Ruby
An Error Occurred While Installing Debugger-Linecache (1.1.1), and Bundler Cannot Continue
Ruby Singleton Methods with (Class_Eval, Define_Method) VS (Instance_Eval, Define_Method)
Suitability of Rails, Padrino, and Sinatra for Building a Prepaid Mobile Service