首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Blacklight中为文档标题添加自定义逻辑?

如何在Blacklight中为文档标题添加自定义逻辑?
EN

Stack Overflow用户
提问于 2021-01-29 19:48:11
回答 1查看 37关注 0票数 1

我希望根据文档的内容显示一个自定义标题,而不是定义单个title_field。根据Blacklight Wiki,这可以通过将config.index.document_presenter_class设置为自定义演示者来实现:

代码语言:javascript
复制
# app/controllers/catalog_controller.rb
class CatalogController < ApplicationController
  ...
  configure_blacklight do |config|
    ...
    config.index.document_presenter_class = MyPresenter
    ...
  end
  ...
end

其中,自定义演示者覆盖label方法:

代码语言:javascript
复制
# app/presenters/my_presenter.rb
class MyPresenter < Blacklight::IndexPresenter
  def label(field_or_string_or_proc, opts = {})
    # Assuming that :main_title and :sub_title are field names on the Solr document.
    document.first(:main_title) + " - " + document.first(:sub_title)
  end
end

当我这样做时,我的自定义标签方法似乎没有被调用,我通过添加一条puts语句和一个调试器断点检查了这一点。

有没有什么是我可能遗漏的,或者是另一种显示自定义文档标题的方式?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-01-29 21:28:51

覆盖我的presenter类中的heading方法对我有效:

代码语言:javascript
复制
# app/presenters/my_presenter.rb
class MyPresenter < Blacklight::IndexPresenter
  def heading
    # Assuming that :main_title and :sub_title are field names on the Solr document.
    document.first(:main_title) + " - " + document.first(:sub_title)
  end
end
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65953879

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档