asb asb - 1 year ago 277
Ruby Question

Errno::ENOENT (No such file or directory @ rb_sysopen

I want to write someting [sic] to a file.

# where userid is any intger [sic]
path = Rails.root + "public/system/users/#{}/style/img.jpg", 'wb') do |file|

When this code is executed, m [sic] getting this error. i [sic] know this folder doesn't exist, but
mode creates a new file if it doesn't exist.

Why is this not working?

Answer Source, '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',, 'style'

FileUtils.mkdir_p(path) unless File.exist?(path), 'img.jpg'), 'wb') do |file|
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download