Search code examples
ruby-on-railsrails-activestorage

active_storage url_for producing unusable urls


My Rails 6.x app has been using ActiveStorage, attaching various files to my models and it has been working fine. But at some point, the url_for method started producing urls that are unusable (i.e. the server returns a 404 when I try to access the url returned by url_for). The url's are broken in both my dev and prod environments which are using Disk and S3 services as defined in my storage.yml respectively.

To try to figure out the problem, I created a new app with the same versions of Rails (6.0.3.2), aws-sdk, etc. and made a simple model with an attachment, attached in the same way, and everything seems to work fine. So I am guessing that there must be some cfg issue that I missed, but I've not been able to find it.

In both cases, the image file gets created in the location specified in storage.yml and the url's generated both look quite similar, but in the first case; accessing the url produces a 404, but in the new app, it works just fine (i.e. the url returns the expected image)

For reference, I've posted below the code used to create the models and attach images, in case that is of interest.

Any thoughts on what I may have mis-configured or forgotten to migrate appreciated!

Broken app:

liss.image.attach(io: File.open('./test/fixtures/files/liss-image.jpg'), filename: 'liss-image.jpg')

url_for liss.image
http://localhost:3000/rails/active_storage/blobs/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBCZz09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--f893bcbb165cd1e89d79dc28ac2eed7236172543/liss-image.jpg

Working app:

user = User.create!(first_name: 'John', last_name: 'Appleseed', email: '[email protected]')
user.image.attach(io: File.open('./test/fixtures/files/liss-image.jpg'), filename: 'liss-image.jpg')

url_for user.image
http://localhost:3000/rails/active_storage/blobs/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBCdz09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--047687b69b8e5877cbfdd93cd6128ddd80d41141/liss-image.jpg

Solution

  • Is there a rails/active_storage/blobs in routes?

    Try checking the routes with rails routes.