Home > Software design >  Can't read image path: No such file or directory @ rb_sysopen - /assets/
Can't read image path: No such file or directory @ rb_sysopen - /assets/

Time:01-03

I have an image named test.jpg in my assets/images folder.

I'm trying to read the image in my controller:

    image_path = ActionController::Base.helpers.asset_path("test.jpg")
    image_data = File.read(image_path)

I get the following error:

No such file or directory @ rb_sysopen - /assets/test-80cc818a7ee3f5d3cab23996fb09f4685b38b78258084a1ff23eca1c626646f6.jpg

Any ideas? Why is it appending that code to my image url? Can I get rid of it so it can read the original image url?

Thanks!

CodePudding user response:

So, basically asset_path returns a URL-path, relative to the root domain - ie. "/assets/test-{SHADigest}.jpg".
That path is not a path for the file system, that File.read will accept, therefore, on unix systems you're trying to access the file system at /assets/xyz, like had it been /home/xyz-path.

What you can do, is either read from file system, raw file (Ignoring any thing the asset pipeline may or may not be set up to do)

image_path = Rails.root.join("assets", "images", "test.jpg")
image_data = File.read(image_path) # Maybe check with File.exist?(image_path) as well.

Or you can read from its own webserver, with any HTTP Client tool (RestClient, Net::HTTP or alike), and use asset_url instead of asset_path.
This won't work in rails console unless the server is "accidentally" running, and reachable on config.asset_host from where ever the rails console is running.

  • Related