To add a "Generate" button for creating the identifier and token in both the Index and Show views of your ActiveAdmin panel, follow these steps. This will allow users to trigger the generation of these fields dynamically and display the results without reloading the page.
Step 1: Add a Controller Action to Generate Credentials
First, add a new member_action in your ActiveAdmin registration to handle generating the identifier and token.
# In your ActiveAdmin.register CampaignPartner block:
member_action :generate_credentials, method: :post do
# Generate identifier and token
@campaign_partner = CampaignPartner.find(params[:id])
@campaign_partner.identifier ||= @campaign_partner.name.parameterize(separator: '-')
bearer_token = SecureRandom.hex
hash_token = Rails.application.credentials.secret_key_base
@campaign_partner.token ||= OpenSSL::HMAC.hexdigest('SHA256', hash_token, bearer_token)
@campaign_partner.save!
# Return JSON response for AJAX
render json: {
identifier: @campaign_partner.identifier,
token: @campaign_partner.token
}
end
Step 2: Update the Index View with a "Generate" Button
Add a button to the Index view that triggers the generation and updates the values dynamically:
index do
selectable_column
column :id
column :name
column :identifier do |cp|
if cp.identifier.present?
cp.identifier
else
# Show a button to generate
link_to("Generate", generate_credentials_admin_v1_campaign_partner_path(cp),
method: :post,
remote: true,
class: "button small") do
"Generate"
end
end
end
column :token do |cp|
if cp.token.present?
cp.token
else
# Show a button to generate
link_to("Generate", generate_credentials_admin_v1_campaign_partner_path(cp),
method: :post,
remote: true,
class: "button small") do
"Generate"
end
end
end
# ... (rest of your columns)
actions defaults: true do |cp|
# Add a separate "Generate" action button if needed
item "Generate", generate_credentials_admin_v1_campaign_partner_path(cp), method: :post, class: "button small", remote: true if cp.identifier.blank? || cp.token.blank?
end
end
Step 3: Update the Show View with a "Generate" Button
Add a button to the Show view to generate the credentials:
show do |cp|
attributes_table do
row :id
row :name
row :identifier do
if cp.identifier.present?
cp.identifier
else
link_to("Generate", generate_credentials_admin_v1_campaign_partner_path(cp),
method: :post,
remote: true,
class: "button small")
end
end
row :token do
if cp.token.present?
cp.token
else
link_to("Generate", generate_credentials_admin_v1_campaign_partner_path(cp),
method: :post,
remote: true,
class: "button small")
end
end
# ... (rest of your rows)
end
end
Step 4: Add JavaScript to Update Values Dynamically
Create a JavaScript file (e.g., app/assets/javascripts/admin/campaign_partners.js) to handle the AJAX response and update the UI without a page reload:
$(document).on('click', 'a[data-remote][method=post]', function(e) {
e.preventDefault();
const $link = $(this);
const url = $link.attr('href');
$.ajax({
url: url,
type: 'POST',
dataType: 'json',
success: function(response) {
// Update the identifier and token fields in the index/show view
if ($link.closest('td').find('.index-value').length) {
// For index view
$link.closest('td').html(response.identifier || response.token);
} else {
// For show view
$link.closest('tr').find('.index-value').html(response.identifier || response.token);
}
// Hide the "Generate" button after generation
$link.hide();
},
error: function(error) {
alert('Failed to generate credentials. Please try again.');
}
});
});
Step 5: Ensure Routes Are Configured
Make sure the generate_credentials action is properly routed. In your config/routes.rb, ensure ActiveAdmin's routes are defined:
# config/routes.rb
ActiveAdmin.routes(self)
ActiveAdmin automatically handles member actions like generate_credentials, so no additional routes are needed.
Step 6: Secure the Action (Optional but Recommended)
Add authorization checks to ensure only authorized users can generate credentials:
controller do
def generate_credentials
# Add authorization logic here
authorize! :update, @campaign_partner
# ... (existing code)
end
end
How It Works
-
Index/Show Views:
- If
identifier or token is nil, a "Generate" button appears.
- Clicking the button sends an AJAX POST request to the
generate_credentials action.
-
Controller Action:
- Generates the
identifier and token using your logic.
- Returns the new values as JSON.
-
JavaScript:
- Updates the DOM with the generated values and hides the "Generate" button.
This setup allows users to dynamically generate and display the identifier and token without leaving the page. The credentials are stored in the database and persisted for future access.