Breadcrumbs list in ActiveAdmin shows wrong name when using friendly_id

妖精的绣舞 提交于 2021-01-01 07:13:52

问题


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?


回答1:


From ActiveAdmin source code, which can be found 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!




回答2:


Thanks for @mark-merrit, by this code breadcrumbs shows proper company name.

app/admin/companies.rb

ActiveAdmin.register Post do
  breadcrumb do
    links = [link_to('Admin', admin_root_path), link_to('Companies', admin_companies_path)]
    if %(show edit).include?(params['action'])
      links << link_to(company.name, admin_company_path)
    end
    links
  end
end

Maybe there is a better implementation for parents path in the breadcrumbs. Let me know if you know about it.




回答3:


Does prepending :code to config.display_name in initializers/active_admin.rb work for you?



来源:https://stackoverflow.com/questions/51200799/breadcrumbs-list-in-activeadmin-shows-wrong-name-when-using-friendly-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!