- To preview pdf files you need to install
mupdforPoppler. - To preview video files you need to install
ffmpeg. - To preview Office files as pictures you need to install activestorage-office-previewer by basecamp
Add administrate-field-active_storage and mini_magick to your Gemfile (rails 6):
gem'administrate-field-active_storage'gem"image_processing"for rails 5.x use the following
gem'administrate-field-active_storage',"0.1.8"Install:
$ bundle install
Assuming your model name is Model and field name is attachment
classModelDashboard < Administrate::BaseDashboardATTRIBUTE_TYPES={attachment: Field::ActiveStorage,}# ...Then add :attachment to FORM_ATTRIBUTES and SHOW_PAGE_ATTRIBUTES.
Adding :attachmentCOLLECTION_ATTRIBUTES will work but will probably look too big.
Assuming your model name is Model and field name is attachments the process is identical the only issue is that the form field isn't being permitted, in order to permit it we apply the following method to the dashboard:
classModelDashboard < Administrate::BaseDashboardATTRIBUTE_TYPES={attachments: Field::ActiveStorage,}# ...FORM_ATTRIBUTES={#...:attachments}# permitted for has_many_attacheddefpermitted_attributessuper + [:attachments=>[]]endI 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.
In order to prevent N+1 queries from active storage you have to modify your admin model controller, below an example for a model called User and with attached avatars
moduleAdminclassUsersController < ApplicationControllerdefscoped_resourceresource_class.with_attached_avatarsendendendAdministrate::Field::ActiveStorage expects the presence of a route
DELETE /<namespace>/<resource>/:id/:attachment_name, which will receive an optional parameter
attachment_id in the case of an ActiveStorage::Attached::Many. For instance:
# routes.rb
...
namespace:admindo
...
resources:usersdodelete:avatars,on: :member,action: :destroy_avatarendend# app/controllers/admin/users_controller.rbmoduleAdminclassUsersController < ApplicationController# For illustrative purposes only.## **SECURITY NOTICE**: first verify whether current user is authorized to perform the action.defdestroy_avataravatar=requested_resource.avatars.find(params[:attachment_id])avatar.purgeredirect_back(fallback_location: requested_resource)endendendFor has_one_attached cases, you will use:
# routes.rb
...
namespace:admindo
...
resources:usersdodelete:avatar,on: :member,action: :destroy_avatarendend# app/controllers/admin/users_controller.rbmoduleAdminclassUsersController < ApplicationController# For illustrative purposes only.## **SECURITY NOTICE**: first verify whether current user is authorized to perform the action.defdestroy_avataravatar=requested_resource.avataravatar.purgeredirect_back(fallback_location: requested_resource)endendendThis 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:
# routes.rbdelete:custom_user_avatar_destroy,to: 'users#destroy_avatar'# user_dashboard.rbclassUserDashboard < Administrate::BaseDashboardATTRIBUTE_TYPES={avatars: Field::ActiveStorage.with_options(destroy_url: procdo |namespace,resource,attachment|
[:custom_user_avatar_destroy,{attachment_id: attachment.id}]end),# ...end# ...endVarious options can be passed to Administrate::Field::ActiveStorage#with_options
as illustrated below:
classModelDashboard < Administrate::BaseDashboardATTRIBUTE_TYPES={attachments: Field::ActiveStorage.with_options(index_display_preview: false),# ...}# ...endDisplay attachment preview.
Defaults to true.
Displays the first attachment (which is the only attachment in case of has_one)
in the index action.
Defaults to true.
Indicate the size of the image preview for the index and show actions, respectively.
Refer to mini_magic#resize_to_limit
for documentation.
Default to [150, 150] and [800, 800], respectively.
Displays the number of attachments in the index action.
Defaults to true if number of attachments is not 1.
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.
You can see translation example here.
- 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
- contributers are welcome (code, suggestions, and bugs).
- please document your code.
- add your name to the
contribute.md.
Based on the Administrate::Field::Image template, and inspired by Administrate::Field::Paperclip.