I have developed the sign,sign_up and log_out using devise.. Here i am trying to select one or multiple checkbox to do following actions:block,unblock,delete..After blocking a user he/she can’t login and hit status will be updated to block..if any of the user unblock him then he can login..Deleted user can register..
This is my view code:
<div class="card">
<div class="card-header">
User Management
</div>
<div class="card-body">
<%= form_tag(admin_users_path, method: :post, id: "bulk-actions-form") do %>
<table class="table table-light table-hover table-striped">
<thead class="table-dark">
<tr>
<th><input type="checkbox" id="select-all" onclick="toggleAllCheckboxes()"></th>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Last Login Time</th>
<th>Registration Time</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<% @users.each do |user| %>
<tr>
<td><input type="checkbox" name="user_ids[]" value="<%= user.id %>"></td>
<td><%= user.id %></td>
<td><%= user.username %></td>
<td><%= user.email %></td>
<td><%= user.last_sign_in_at %></td>
<td><%= user.created_at %></td>
<td><%= user.status %></td>
</tr>
<% end %>
</tbody>
</table>
<div class="button-group">
<button type="button" class="btn btn-danger" onclick="bulkAction('block')">Block</button>
<button type="button" class="btn btn-primary" onclick="bulkAction('unblock')">Unblock</button>
<button type="button" class="btn btn-warning" onclick="bulkAction('delete')">Delete</button>
</div>
<% end %>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<script>
// Function to handle bulk actions
function bulkAction(action) {
var form = document.getElementById("bulk-actions-form");
var actionUrl;
switch (action) {
case 'block':
actionUrl = "/admin/users/bulk_actions/block";
break;
case 'unblock':
actionUrl = "/admin/users/bulk_actions/unblock";
break;
case 'delete':
actionUrl = "/admin/users/bulk_actions/delete";
break;
}
form.action = actionUrl;
form.submit();
}
// Function to toggle all checkboxes based on the header checkbox
function toggleAllCheckboxes() {
var checkboxes = document.querySelectorAll("input[name='user_ids[]']");
var selectAllCheckbox = document.getElementById("select-all");
checkboxes.forEach(function(checkbox) {
checkbox.checked = selectAllCheckbox.checked;
});
// Set indeterminate state if only some checkboxes are checked
selectAllCheckbox.indeterminate = checkboxes.length > 0 && !Array.from(checkboxes).every(checkbox => checkbox.checked) && !Array.from(checkboxes).every(checkbox => !checkbox.checked);
}
</script>
Controller :
class Admin::UsersController < ApplicationController
before_action :restrict_to_post_or_delete_request, only: [:bulk_actions, :block, :unblock, :bulk_delete]
def index
@users = User.all
end
def bulk_actions
action = params[:action_type]
case action
when 'block'
User.where(id: params[:user_ids]).update_all(status: 'blocked')
redirect_to admin_users_path, notice: 'Users blocked successfully'
when 'unblock'
User.where(id: params[:user_ids]).update_all(status: 'active')
redirect_to admin_users_path, notice: 'Users unblocked successfully'
when 'delete'
User.where(id: params[:user_ids]).destroy_all
redirect_to admin_users_path, notice: 'Users deleted successfully'
else
redirect_to admin_users_path, alert: 'Invalid action'
end
end
def block
user = User.find(params[:id])
user.update(status: 'blocked')
redirect_to admin_users_path, notice: 'User blocked successfully'
end
def unblock
user = User.find(params[:id])
user.update(status: 'active')
redirect_to admin_users_path, notice: 'User unblocked successfully'
end
def bulk_delete
User.where(id: params[:user_ids]).destroy_all
redirect_to admin_users_path, notice: 'Users deleted successfully'
end
private
def restrict_to_post_or_delete_request
unless request.post? || request.delete?
redirect_to root_path, alert: 'Invalid request'
end
end
end
Routes:
namespace :admin do
resources :users do
post :bulk_actions, on: :collection
post :block, on: :member
post :unblock, on: :member
delete :bulk_actions, on: :collection # Define delete route for bulk_actions
end
end