0

I am using Rails 7 Active admin I have Project that is Workshop Project that is related to organizing workshops, booking them etc. I am using Friendly ID Gem . The url looks like http://127.0.0.1:3000/workshops/ai-the-new-era using friendly id. Now i am using active_admin and whenever i try to use same show or edit or delete route in http://127.0.0.1:3000/admin/workshops/rails-hotwire-the-tew-era-of-rails I get this error

ActiveRecord::RecordNotFound in Admin::WorkshopsController#show Couldn't find Workshop with 'id'=rails-hotwire-the-tew-era-of-rails

    statement.execute([id], connection).first ||
      raise(RecordNotFound.new("Couldn't find #{name} with '#{key}'=#{id}", name, key, id))
  end

My code for admin/workshop.rb

ActiveAdmin.register Workshop do
  filter :registeration_fee_eq, label: 'Registration Fee'
  
  permit_params :name, :description, :start_date, :end_date, :start_time, :end_time, :total_seats, :available_seats, :registeration_fee, :slug
  form do |f|
    f.inputs "Workshop Details" do
      f.input :name
      f.input :description
      f.input :start_date
      f.input :end_date
      f.input :start_time
      f.input :end_time
      f.input :total_seats
      f.input :available_seats
      f.input :registeration_fee
      f.input :slug
      # Add the image file field
      f.input :image, as: :file
    end
    f.actions
  end

  index do
    column :id
    column :name
    column :total_seats
    column :available_seats
    column :registeration_fee
    actions
  end

  show do
    attributes_table do
      row :id
      row :name
      row :description
      row :registeration_fee do |workshop|
        workshop.registeration_fee
      end
    end
  end

end

I tried Many things like removing friendly id but since i need user friendly url in my user side that's why I am not doing that

2 Answers2

1

You need to override the find_resource method in the ActiveAdmin controller.

ActiveAdmin.register Workshop do
  controller do
    def find_resource
      begin
        scoped_collection.where(slug: params[:id]).first!
      rescue ActiveRecord::RecordNotFound
        scoped_collection.find(params[:id])
      end
    end
  end
end

Something like this should work.

Kevin Maze
  • 86
  • 4
0

In admin/workshop.rb:

controller do
  def find_resource
    Workshop.friendly.find(params[:id])
  end
end

make sure your model is also properly setup to have unique slugs

class Workshop < ApplicationRecord
  extend FriendlyId

  validates :slug, uniqueness: true
  friendly_id :name, use: [:slugged, :history]

end

side note: your slug has a spelling mistake

rails-hotwire-the-tew-era-of-rails. you probably meant

rails-hotwire-the-new-era-of-rails <--- new vs tew for title

Denis S Dujota
  • 543
  • 5
  • 13