本文介绍了使用friendly_id时,ActiveAdmin中的面包屑列表显示错误的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 Company 的模型,它具有 code.该列用于 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 无法识别 friendly_id,因此我不得不像这样覆盖 find_resource 方法:

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

使用此代码,我可以通过 ActiveAdmin 编辑模型属性,但编辑页面中的面包屑列表显示错误的公司名称.(即使用id,而不是code)

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)

我可以在何处以及如何配置以同时使用 ActiveAdminfriendly_id?

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

推荐答案

来自 ActiveAdmin 源代码,可在 lib/active_admin/dsl.rb 中找到

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

由于它是在控制器中执行的,因此您可以使用自定义的 find_resource 方法根据自己的喜好对其进行配置!

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

这篇关于使用friendly_id时,ActiveAdmin中的面包屑列表显示错误的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 00:07