支持在管理中使用主动存储
mupdf or Poppler.ffmpeg.Add administrate-field-active_storage and image_processing 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 installhas_one_attached:Assuming your model name is Model and field name is attachment
class ModelDashboard []]
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. 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:
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
endFor has_one_attached cases, you will use:
# 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
endThis 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.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
# ...
endVarious options can be passed to Administrate::Field::ActiveStorage#with_options
as illustrated below:
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.
Displays the number of attachments in the index action.
Defaults to true if number of attachments is not 1.
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.
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.
cd test_app && bundle && bundle exec rails test.contribute.md.Based on the Administrate::Field::Image template, and inspired by Administrate::Field::Paperclip.
暂无开放 Issues,或尚未同步最近议题。