Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Breadcrumbs list in ActiveAdmin shows wrong name when using friendly_id

I have a model named Company that has code. The column is used for friendly_id.

class Company < ActiveRecord::Base
  extend FriendlyId
  friendly_id :code, use: :slugged
end

ActiveAdmin doesn't recognize friendly_id, so that I had to override find_resource method like this:

ActiveAdmin.register Company do
  controller do
    def find_resource
      scoped_collection.friendly.find(params[:id])
    end
  end
end

With this code I can edit the model attributes by ActiveAdmin, but breadcrumbs list in edit page shows wrong company's name. (That is using id, instead of code)

Where and how can I configure to use ActiveAdmin and friendly_id at the sametime?

like image 370
ironsand Avatar asked Aug 31 '25 17:08

ironsand


2 Answers

Thanks for the idea to @mark-merrit, using the code below you can dynamically customize breadcrumbs:

ActiveAdmin.register Company do
  breadcrumb do
    links = [link_to('Admin', admin_root_path), link_to('Companies', admin_companies_path)]
    if %(show edit update).include?(params['action'])
      links << link_to(resource.name, admin_company_path)
    end
    links
  end
end
like image 135
ironsand Avatar answered Sep 02 '25 17:09

ironsand


breadcrumb method is defined in lib/active_admin/dsl.rb

# Rewrite breadcrumb links.
# Block will be executed inside controller.
# Block must return an array if you want to rewrite breadcrumb links.
#
# Example:
#   ActiveAdmin.register Post do
#     breadcrumb do
#       [
#         link_to('my piece', '/my/link/to/piece')
#       ]
#     end
#   end
#
def breadcrumb(&block)
  config.breadcrumb = block
end

Since it is executed in the controller, you can use your custom find_resource method to configure it to your liking!

like image 22
Mark Merritt Avatar answered Sep 02 '25 15:09

Mark Merritt