8

I have an Employee model with an Avatar. I can attach an image to the avatar, but whenever I try to display the image, the

url_for(@employee.avatar)

produces a dead link. All I'm seeing is the value from the alt attribute from the tag. The image tag I'm getting is the following

<img src="/rails/active_storage/blobs/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBDZz09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--4786aa4d9d82d8f8d572b857965088b20fcb2f49/Portrait.jpg" 
alt="Thumbnail">

And I know the image has been properly attached. When I call the following, I get this result:

@employee.avatar
=> #<ActiveStorage::Attached::One:0x00007ff7e9ba41c0 @name="avatar",
@record=#<Employee id: 4, first_name: "Meda", last_name: "Burgdorf",  
created_at: "2019-03-03 23:03:00", updated_at: "2019-03-03 23:17:56">, 
@dependent=:purge_later> 

as I can see the image in the storage directory Screenshot file structure

Help is highly appreciated. Can anyone help me display the saved image.

Here is my setup.

class Employee < ApplicationRecord
   has_one_attached :avatar
   ...
end

Content of my storage.yml file

local:
   service: Disk
   root: <%= Rails.root.join("storage") %>

My the migrations from Active Storage are migrated. See my schema.rb file

ActiveRecord::Schema.define(version: 2019_03_03_211537) do

  create_table "active_storage_attachments", force: :cascade do |t|
    t.string "name", null: false
    t.string "record_type", null: false
    t.integer "record_id", null: false
    t.integer "blob_id", null: false
    t.datetime "created_at", null: false
    t.index ["blob_id"], name: "index_active_storage_attachments_on_blob_id"
    t.index ["record_type", "record_id", "name", "blob_id"], name: "index_active_storage_attachments_uniqueness", unique: true
  end

  create_table "active_storage_blobs", force: :cascade do |t|
    t.string "key", null: false
    t.string "filename", null: false
    t.string "content_type"
    t.text "metadata"
    t.bigint "byte_size", null: false
    t.string "checksum", null: false
    t.datetime "created_at", null: false
    t.index ["key"], name: "index_active_storage_blobs_on_key", unique: true
  end
fydelio
  • 932
  • 1
  • 8
  • 22
  • `Started GET "/rails/active_storage/blobs/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBDUT09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--76027e62bac321f7684b895d630ad9ecae8af821/CROPPED-Eva_Lottery.jpg" for 127.0.0.1 at 2019-03-04 22:52:51 +0100` To my understanding no error there. – fydelio Mar 04 '19 at 21:55
  • 2
    @muistooshort: I found the error, though I don't quite understand why this broke the solution! In my routes.rb all the way at the end I had `#get '*path' => redirect('/')` which catched all unknown routes and routed it back to roots. By removing this line I now see the images. – fydelio Mar 04 '19 at 22:14
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/189420/discussion-between-fydelio-and-mu-is-too-short). – fydelio Mar 04 '19 at 22:19

1 Answers1

18

Since Active Storage appends the routes in the master route file, so they come after you catch all the routes. Better you can escape the active storage routes like

get '*path', to: redirect('/'), constraints: lambda { |req|
  req.path.exclude? 'rails/active_storage'
}
V K Singh
  • 1,134
  • 9
  • 14
  • In which file do we add this and where? – Diana Mar 21 '20 at 14:37
  • 1
    This will be added in routes.rb file under config folder. Also this will be added at the end of the file. – V K Singh Apr 20 '20 at 08:35
  • This is a good hack but unfortunately opens up unfixable routing errors for anything with that prefix. For example, `www.example.com/rails/active_storage/404` - app-breaking, unhandled routing error. – AFOC Dec 09 '20 at 16:50