Administrate: Deal with Active Storage attachments.

Created on 6 Nov 2018  路  10Comments  路  Source: thoughtbot/administrate

rails - 5.2.1
Active Storage - installed

class Model < ApplicationRecord
  has_one_attached :logo
  has_many_attached :documents
  has_many_attached :pictures
end

get
uninitialized constant LogoAttachmentDashboard
immediately after installation.

screenshot 2018-11-06 8 48 01

dependencies maintenance

Most helpful comment

There is this gem and with the default settings it _Just Works鈩 for me.

All 10 comments

https://github.com/thoughtbot/administrate/issues/1100

Administrative doesn't seem to work if you're using Active Storage.

Here is what I did to get this to work:

Add Attachment Dashboard

# app/dashboards/attachment_dashboard.rb

require "administrate/base_dashboard"

module ActiveStorage
  class AttachmentDashboard < Administrate::BaseDashboard
    ATTRIBUTE_TYPES = {
      id: Field::Number,
      name: Field::String,
      record: Field::Polymorphic,
      blob: Field::BelongsTo,
      created_at: Field::DateTime,
    }.freeze

    COLLECTION_ATTRIBUTES = [
      :id,
      :name,
      :record,
      :blob,
      :created_at
    ].freeze

    SHOW_PAGE_ATTRIBUTES = [
      :id,
      :name,
      :record,
      :blob,
      :created_at,
    ].freeze

    FORM_ATTRIBUTES = [
      :name,
    ].freeze

  end
end

class AttachmentDashboard < ActiveStorage::AttachmentDashboard
end

Add Attachment Model

# app/models/attachment.rb

class Attachment < ActiveStorage::Attachment
end

Follow above steps for Blob

In your routes add these inside the admin namespace

# config/routes.rb

namespace :admin do
  ...
  resources :attachments
  resources :blobs
  ...

This will get you past the errors, but it still needs customization. Will try to post a more complete answer later.

Hi! I'm having the same issue with Rails 5.2.2.

@Tashows I did all these steps but seems does not work.

NameError at /admin/jobs uninitialized constant LogoAttachmentDashboard

@SashkaSh I edited my answer with some changes that ended up working for me. Haven't had the time to fully work on that yet.

My solution was to:

  • Create a custom AttachmentField for active storage files as per docs
  • Populate the field as per snippet below
  • Define the Attachment class at the top of my dashboard file. I didn't want to clutter my models with a work around file
# app/fields/attachment_field.rb
require "administrate/field/base"

class AttachmentField < Administrate::Field::Base
  def to_s
    return "Not attached" unless attached?
    service_url
  end

  private

  def attached?
    data.record.send("#{data.name}").attached?
  end

  def service_url
    data.record.send("#{data.name}").service_url
  rescue => ex
    Raven.capture_exception(ex)
    "error"
  end
end
# app/dashboards/MODEL_WITH_ATTACHEMENTS

# workaround for active storage attachements as per
# https://github.com/thoughtbot/administrate/issues/1236
class Attachment < ActiveStorage::Attachment
end

# dashboard definition stuff
.
.
.

There is this gem and with the default settings it _Just Works鈩 for me.

There is this gem and with the default settings it _Just Works鈩 for me.

That gem looks good alright. I opted against using it as I didn't need many of its features.
My only concerns were:

  • Is there an attachment
  • What is its url

Yep, I actually like the preview -- which is the only real other functionality, but because it tries to handle videos, audio, images etc there is "a lot".

I'm going to close this one to merge discussion of ActiveStorage to #1560, which digs into some more specific issues.

It seems like the suggestion of using the field plugin is likely the right move for most cases, and areas where that isn't working we can discuss that separately.

Was this page helpful?
0 / 5 - 0 ratings