przbadu przbadu - 2 months ago 25
Ruby Question

ActiveAdmin with friendly id

I am using

friendly_id
in my rails 4 application with
slug
. Now I am using active_admin gem.

Problem:

When I click on show link from active admin for
Group
resource, It is throwing
RecordNotFound
exception

ActiveRecord::RecordNotFound at /admin/groups/username20-s-group-1


I guess, I need to override some of the
active_admin
default functions?

Answer

There are cases, when application has quit a few resources, hence in order to keep it DRY there is a nice solution requiring few lines of code for whole application - simply override activeadmin's resources controller.

Create config/intializers/active_admin.rb file and put the following there:

  ActiveAdmin::ResourceController.class_eval do
    def find_resource
      return scoped_collection.where(slug: params[:id]).first! if resource_class.is_a?(FriendlyId)

      scoped_collection.where(id: params[:id]).first!
    end
  end