Administrate Field Button enhances your Administrate dashboard with customizable buttons. This gem provides a flexible way to add buttons to your resource views.
gem 'administrate-field-button', github: 'jubilee2/administrate-field-button'
bundle install
class MyResourceDashboard < Administrate::BaseDashboard
ATTRIBUTE_TYPES = {
my_button: Field::Button,
my_button2: Field::Button.with_options(
label_of_button: 'Foo action',
method: :post,
branch: ->(field) { field.resource.active? },
confirm: false
)
}.freeze
end
class Book < ApplicationRecord
attr_reader :my_button, :my_button2
end
namespace :admin do
resources :my_resource do
member do
post :my_button
end
end
end
def my_button
# Custom logic here
flash[:notice] = 'Action done successfully'
redirect_back fallback_location: root_path
end
-
label_of_button (String): Button label.
-
method (Symbol): HTTP method (:post, :put, :delete). Default: :post.
-
branch (Proc): Conditional display based on resource attributes.
-
confirm (Boolean): Confirmation dialog. Default:
false
.
my_button: Field::Button.with_options(
label_of_button: 'Custom Action',
method: :patch,
branch: ->(field) { field.resource.published? && field.resource.approved? },
confirm: true
)
We welcome bug reports and pull requests on GitHub.
This gem is available as open source under the terms of the MIT License.
For more information on Administrate and custom fields, refer to the following resources: