active admin with friendly_id
# Step 1: Install the gems
# Add the following gems to your Gemfile:
gem 'friendly_id'
gem 'activeadmin'
# Then run:
bundle install
# Step 2: Set up FriendlyId
# Generate a migration to add a slug column to your model:
rails g migration AddSlugToModel slug:uniq
# Run the migration:
rails db:migrate
# In your model file, add the following:
class YourModel < ApplicationRecord
extend FriendlyId
friendly_id :attribute_to_use_for_slug, use: :slugged
end
# Replace :attribute_to_use_for_slug with the attribute you want to use for the slug.
# Step 3: Update Active Admin
# In your Active Admin resource file for the model, add the following:
ActiveAdmin.register YourModel do
permit_params :attribute_to_use_for_slug, :other_attributes
form do |f|
f.inputs do
f.input :attribute_to_use_for_slug
# Add other input fields for your model attributes
end
f.actions
end
end
# Step 4: Customize Show Page (Optional)
# To display the slug on the Active Admin show page, modify the show block in the resource file:
show do
attributes_table do
# Other attributes
row :attribute_to_use_for_slug
end
end
These steps guide you through setting up FriendlyId with Active Admin in a Ruby on Rails application. Adjustments are made to both the model and the Active Admin resource file to incorporate slugs and enable their management within the admin interface.