1

I'm working on a Rails 6 app and have a blog model with a slug generated by FriendlyId gem. I added Active admin and I had to add this code to config/initializers/active_admin.rb to make it work properly and search by the id and not the slug.

  ActiveAdmin::ResourceController.class_eval do
    def find_resource
      resource_class.is_a?(FriendlyId) ? scoped_collection.friendly.find(params[:id]) : scoped_collection.find(params[:id])
    end
  end

Then I added paranoia to soft-deleted my post.

I added the gem 'paranoia', '~> 2.4', '>= 2.4.2' and run

rails g migration AddDeletedAtToPosts deleted_at:datetime:index

In the post model:

app/model/post.rb

class Post < ApplicationRecord
  acts_as_paranoid

  extend FriendlyId
  friendly_id :title, :body use: :slugged

  validates_presence_of :title, :body
end

This works fine in the view and in the rails console, I have no problem viewing and destroying the posts.

but I wanted to add the soft delete to my active-admin.

I installed gem "active_admin_paranoia" , '~> 1.0.11' and added

app/admin/blog.rb

ActiveAdmin.register Blog do
  active_admin_paranoia

  actions :all, except: [:edit]
  permit_params :title, :body, :slug
end

Now when trying to view or archive from the active-admin dashboard I'm getting

Started DELETE "/admin/posts/my-post-0" for ::1 at 2020-07-16 15:14:26 -0300
Processing by Admin::postsController#destroy as HTML
  Parameters: {"authenticity_token"=>"aaaaaaa", "id"=>"my-post-0"}
  AdminUser Load (0.3ms)  SELECT "admin_users".* FROM "admin_users" WHERE "admin_users"."id" = $1 ORDER BY "admin_users"."id" ASC LIMIT $2  [["id", 1], ["LIMIT", 1]]
  Post Load (0.5ms)  SELECT "posts".* FROM "posts" WHERE "posts"."id" = $1 ORDER BY "posts"."id" ASC LIMIT $2  [["id", nil], ["LIMIT", 1]]
Completed 404 Not Found in 8ms (ActiveRecord: 0.8ms | Allocations: 3691)

  
ActiveRecord::RecordNotFound (Couldn't find Post with [WHERE "posts"."id" = $1]):
  
activerecord (6.0.3.2) lib/active_record/relation/finder_methods.rb:336:in `raise_record_not_found_exception!'
activerecord (6.0.3.2) lib/active_record/relation/finder_methods.rb:127:in `first!'
active_admin_paranoia (1.0.11) lib/active_admin_paranoia/dsl.rb:6:in `find_resource'
activeadmin (2.7.0) lib/active_admin/resource_controller/data_access.rb:90:in `resource'
inherited_resources (1.11.0) lib/inherited_resources/actions.rb:55:in `destroy'
actionpack (6.0.3.2) lib/action_controller/metal/basic_implicit_render.rb:6:in `send_action'
actionpack (6.0.3.2) lib/abstract_controller/base.rb:195:in `process_action'
actionpack (6.0.3.2) lib/action_controller/metal/rendering.rb:30:in `process_action'
actionpack (6.0.3.2) lib/abstract_controller/callbacks.rb:42:in `block in process_action'

How can I fix this?

sara lance
  • 493
  • 4
  • 16

2 Answers2

0

Active admin paranoia has not been updated in over 3 years... so is simply not compatible with new rails 6. you should instead try and use this one paranoia

Good luck

diego
  • 409
  • 4
  • 12
0

Have you checked this PR? It seems to do what you're searching for.

In this PR they replace the search method from where(id: params[:id]).first to public_send(method_for_find, params[:id]) which uses the rails method for searching which will work with friendly id.

https://github.com/raihan2006i/active_admin_paranoia/pull/12/files

Tiago
  • 673
  • 1
  • 8
  • 24