Open brandon-meeks opened 3 days ago
I don't have support for batch actions currently in the admin DSL yet (though it is on the roadmap), so right now the selectable_column
method is a little incomplete.
Having said that, the Hotwire updates I've released today (in particular full support for Stimulus controllers) will make this much easier to wire up manually. I'll be going through that on another project tomorrow. Once I have the process sorted, I'll post here with an outline.
I've just pushed 014dc16950c453cbe1daec44388cc146ed758ce5 which adds a new batch-action
Stimulus controller, as well as an example implementation in the sandbox. As I mentioned, this is by no means a complete and final solution but at least makes batch actions possible right now.
The steps to implement are:
<resource>_admin.rb
file:controller do
def batch_get
ids = params[:ids].split(",")
flash[:message] = { title: "Success!", message: "Performed batch action via GET with #{ids.size} articles." }
redirect_back fallback_location: admin.path
end
def batch_post
ids = params[:ids].split(",")
flash[:message] = { title: "Success!", message: "Performed batch action via POST with #{ids.size} articles." }
redirect_back fallback_location: admin.path
end
end
routes do
collection do
get :batch_get
post :batch_post
end
end
The ids
param containing the selected IDs is a comma-separated array of strings so you'll need to split it as shown above.
index.html.erb
file from here and copy it to app/views/admin/<resources>/index.html
. Then edit to add your batch action buttons within a toolbar:<% toolbar(:secondary) do |t| %>
<%= t.link "Batch Action (GET)", action: :batch_get, style: :info, data: { controller: "batch-action" } %>
<%= t.link "Batch Action (POST)", action: :batch_post, style: :warning, data: { controller: "confirm batch-action", turbo_method: :post } %>
<% end %>
The button trigger should be a link with data-controller="batch-action"
, but POST actions are supported via data-turbo-method="post"
. You can also add the confirm
action for an easy confirmation popover.
I have poured over the source code for the select_column (selectable_column) that can be added to a table, but I have yet to determine what adding that column does or how to provide actions that can be performed once a row is selected. I don't see anywhere to add actions to a table that can be performed on a batch of selected items.