No Such File or Directory @ Rb_Sysopen for External Url/Rails 6.11/Ruby 3

No such file or directory @ rb_sysopen for external URL / Rails 6.11 / Ruby 3

open-uri used to (before Ruby 3.0) overwrite Kernel#open with its own version which also supports reading from external URLs rather than simply opening local files or running commands.

Mixing those two use-cases was quite dangerous and had the potential for serious vulnerabilities if the passed URL was not ensured to be safe everywhere (including third-party code using Kernel#open).

As such, this behavior to overwrite Kernel#open was deprecated in Ruby 2.7 and finally removed in Ruby 3.0. To open an external URL, you can use the following code instead:

URI.open("https://brandemia.org/sites/default/files/inline/images/firefox_logo.jpg")

Ruby Invalid argument @ rb_sysopen

Method :open you call belongs to Kernel class:

> method :open
=> #<Method: Object(Kernel)#open(*)>

I believe you want to call URI.open because you require open-uri gems.

page Nokogiri::HTML(URI.open(url))

Put unique constraint on multiple fields of a model

Yes, you can do it with a regular validates, uniqueness and scope options:

validates :field_one, uniqueness: { scope: [:field_two] }

If you want your own custom validation method, you can do this:

validates :uniqueness_of_fields

private # optionnal
def uniqueness_of_fields
if self.class.exists?(field_one: self[:field_one], field_two: self[:field_two])
self.errors.add(:field_one, "Combination #{self[:field_one]} and #{self[:field_two]} already existings!"
logger :do_your_thing # here is where you want to log stuff
return false
else
return true # validation passes, is unique
end
end

Getting Errno::ENOENT: No such file or directory @ rb_sysopen When trying to open remote file url in Roo gem(Ruby on rails)

Finally following code is worked for me.

spreadsheet = Roo::Spreadsheet.open(open(imported_file.file_url), extension: File.extname(imported_file.file_url).gsub('.','').to_sym) rescue nil

Cannot open file with Ruby Errno::ENOENT: No such file or directory @ rb_sysopen

File.open expects files to be on the disk, it doesn't work with remote files.

Instead you could do use Net::HTTP.get, which will return a string.

 url = URI('https://www.pocket-rocket.io/robots.txt')
Net::HTTP.get(url) # => "User-agent: *\nDisallow: /wp-admin/\nAllow: /wp-admin/admin-ajax.php\n"

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


Related Topics



Leave a reply



Submit