Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
A

administrate-field-active_storage

> 后端框架
Open source

support active storage in administrate

171 stars0 likes3 views
WebsiteGitHub

About

support active storage in administrate

Administrate::Field::ActiveStorage

Things To Know:

  • To preview pdf files you need to install mupdf or Poppler.
  • To preview video files you need to install ffmpeg.
  • To preview Office files as pictures you need to install activestorage-office-previewer by basecamp

How To Use:

Add administrate-field-active_storage and image_processing to your Gemfile (Rails 6+):

ruby
gem "administrate-field-active_storage"
gem "image_processing"

for Rails 5.x use the following

ruby
gem "administrate-field-active_storage", "0.1.8"

Install:

$ bundle install

has_one_attached:

Assuming your model name is Model and field name is attachment

ruby
class ModelDashboard  []]
  end

I know it is not ideal, if you have a workaround please submit a PR.

Note: Rails 6 introduced a new config to determine the behavior on updates to has_many_attached. Setting Rails.application.config.active_storage.replace_on_assign_to_many to true will overwrite any existing values (purging the old ones), and setting it to false will append the new values. Please note that this configuation was deprecated with Rails 7.1

config.active_storage.replace_on_assign_to_many is deprecated and will be removed in Rails 7.1. Make sure that your code works well with config.active_storage.replace_on_assign_to_many set to true before upgrading. To append new attachables to the Active Storage association, prefer using attach. Using association setter would result in purging the existing attached attachments and replacing them with new ones.

This means that in Rails 7 for has_many_attached, whenever a form is submitted, all associations to existing attachments are being removed without the ActiveStorage objects being deleted. This is undesired behavior because you have to re attach every time you update an object and it will result in orphaned ActiveStorage objects. To fix this and to add the ability to add more attachments to an existing set of attachments, follow this workaround. In short, you can create a concern:

…

And then load this concern in your model:

ruby
class Model //:id/:attachment_name`, which will receive an optional parameter
`attachment_id` in the case of an `ActiveStorage::Attached::Many`. For instance:

```rb
# routes.rb
...
namespace :admin do
  ...
  resources :users do
    delete :avatars, on: :member, action: :destroy_avatar
  end
end

# app/controllers/admin/users_controller.rb
module Admin
  class UsersController < ApplicationController

    # For illustrative purposes only.
    #
    # **SECURITY NOTICE**: first verify whether current user is authorized to perform the action.
    def destroy_avatar
      avatar = requested_resource.avatars.find(params[:attachment_id])
      avatar.purge
      redirect_back(fallback_location: requested_resource)
    end
  end
end

For has_one_attached cases, you will use:

rb
# routes.rb
...
namespace :admin do
  ...
  resources :users do
    delete :avatar, on: :member, action: :destroy_avatar
  end
end

# app/controllers/admin/users_controller.rb
module Admin
  class UsersController < ApplicationController

    # For illustrative purposes only.
    #
    # **SECURITY NOTICE**: first verify whether current user is authorized to perform the action.
    def destroy_avatar
      avatar = requested_resource.avatar
      avatar.purge
      redirect_back(fallback_location: requested_resource)
    end
  end
end

This route can be customized with destroy_url. The option expects a proc receiving 3 arguments: the Administrate namespace, the resource, and the attachment. The proc can return anything accepted by link_to:

rb
# routes.rb
delete :custom_user_avatar_destroy, to: 'users#destroy_avatar'

# user_dashboard.rb
class UserDashboard < Administrate::BaseDashboard
  ATTRIBUTE_TYPES = {
    avatars: Field::ActiveStorage.with_options(
      destroy_url: proc do |namespace, resource, attachment|
        [:custom_user_avatar_destroy, { attachment_id: attachment.id }]
      end
    ),
    # ...
  end
  # ...
end

Options

Various options can be passed to Administrate::Field::ActiveStorage#with_options as illustrated below:

rb
class ModelDashboard < Administrate::BaseDashboard
  ATTRIBUTE_TYPES = {
    attachments: Field::ActiveStorage.with_options(index_display_preview: false),
    # ...
  }
  # ...
end

…

rb
 //= link 'administrate-field-active_storage/application.css'

When set, this takes precedence over index_preview_size and show_preview_size.

Setting this to false displays original images instead of variants.

Defaults to nil.

index_display_count

Displays the number of attachments in the index action.

Defaults to true if number of attachments is not 1.

file_field_options

The file_field_options is a configuration option that allows you to customize the behavior and appearance of the file input field used for uploading attachments. You can pass various options to file_field to control its functionality.

The multiple HTML attribute for file input fields is set by default based on ::ActiveStorage::Attached::Many, but can be overridden through this option. The direct attribute to enable direct uploads is set to read from the field options, but can be overridden through this option.

direct

Enables direct upload from the browser to the cloud.

Defaults to false.

Don't forget to include ActiveStorage JavaScript. You can use rails generate administrate:assets:javascripts to be able to customize Administrate JavaScripts in your application.

I18n

You can see translation example here.

Things To Do:

  • upload single file
  • adding image support through url_for to support 3rd party cloud storage
  • use html 5 video element for video files
  • use html audio element for audio files
  • download link to other files
  • preview videos
  • preview pdfs
  • upload multiple files
  • find a way to delete attachments
  • preview office files as pictures

Contribution Guide:

  1. contributors are welcome (code, suggestions, and bugs).
  2. please test your code: cd test_app && bundle && bundle exec rails test.
  3. please document your code.
  4. add your name to the contribute.md.

Based on the Administrate::Field::Image template, and inspired by Administrate::Field::Paperclip.

Issues· 6 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Rubyactivestorageadministraterails

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category后端框架
PricingOpen source

> Related tools

N
Node.js
基于 V8 的 JavaScript 运行时
D
Django
Python 高级 Web 框架
S
Spring Boot
Java 生态主流微服务框架