rails 4: Errno::ENOENT (No such file or directory @ rb_sysopen - /assets/
You're asking for the file from a relative path, which might not always work. Try absolute:
@css_asset_bootstrap = File.open(Rails.root + ActionController::Base.helpers.asset_path('bootstrap.css'), "r").read
Another possibly bigger problem is that Heroku has an ephemeral filesystem, and so any changes you write to it can be obliterated at any time when the dyno is culled.
Peter Devlin
Updated on June 04, 2022Comments
-
Peter Devlin almost 2 years
I've just deployed my app to heroku and I'm having a lot of trouble trying to read some assets.
I am trying to dynamically load some css from multiple files, then recompile them with some changes later. The line that keeps breaking is the first file load:
@css_asset_bootstrap = File.open(ActionController::Base.helpers.asset_path('bootstrap.css'), "r").read
which generates this error:
2015-04-07T23:30:50.098831+00:00 app[web.1]: Errno::ENOENT (No such file or directory @ rb_sysopen - /assets/bootstrap-2d25733981c30e34bd9aa0fb75388f08.css):
I've tried a lot of things, including moving all my assets to aws cloudfront. Is there a way to get around this? Works perfectly in development environment.
Just to confirm a few things. I've successfully precompiled and uploaded the file. The file definately exists as I can see it in
heroku run bash cd /public/assets/
I can also see it when I moved the assets to cloudfront.
Thanks.
edit 1: Not sure if this is important information, but with the file on cloudfront, I can run heroku run bash to start a shell session on heroku. Then I can:
curl http://xxx.cloudfront.net/assets/bootstrap-2d25733981c30e34bd9aa0fb75388f08.css
And get the file ok. I was thinking maybe it was a permissions error but everything is set to public and it all seems to work from heroku to the aws server.
-
vee about 9 yearsAssuming the problem is because in dev env there is no action controller caching by default which is why it works in dev and not in any other env with caching turned on. The problem seems to be the digest changes as you recompile assets but
@css_asset_bootstrap
does not re-run and points to the same old asset resulting in the error. Try updating@css_asset_bootstrap
's value after every compilation. -
Peter Devlin about 9 yearsThanks for your comment vee. The digest in the error is the same as the one in the assets directory. If it was caching, wouldn't the error throw the old file digest and not the current one? The file on the server is bootstrap-2d25733981c30e34bd9aa0fb75388f08.css
-
Peter Devlin about 9 yearsThe asset can also be accessed using
http://myapp.herokuapp.com/assets/bootstrap-2d25733981c30e34bd9aa0fb75388f08.css
-
-
Peter Devlin about 9 yearsThanks for your answer jemminger. I just tried your solution, but still have the same error. I actually tried to add in the absolute path to cloudfront earlier using
"xxx.cloudfront.net#{ActionController::Base.helpers.asset_path('bootstrap.css')}"
which didn't work either. Even though I could copy and past the address from the error (no such file) straight into the browser and see the file! Good point about the ephemeral filesystem, though I'm not worried about it at this point. I dynamically create a temporary file later in the code and upload it to aws S3 where it should be static. -
jemminger about 9 yearsYou can't open a remote file with vanilla
File.open
, you'll have to useopen-uri
for that: ruby-doc.org/stdlib-2.1.0/libdoc/open-uri/rdoc/OpenURI.html -
Peter Devlin about 9 yearsYep that's it. open-uri is the answer. Thanks.