【问题标题】:Filtering belongs_to drop-down in administrate dashboard在管理仪表板中过滤 belongs_to 下拉菜单
【发布时间】:2022-06-17 03:44:42
【问题描述】:

我在 Rails (6.1.5) 中有一个管理仪表板,允许管理员添加入围者并为他们分配项目分配。项目在活动中。创建候选名单时添加事件的下拉菜单来自 event_shortlister_dashboard.rb

的这些片段
ATTRIBUTE_TYPES = {
  shortlister: Field::BelongsTo,
  event: Field::BelongsTo,
  project_allocation: Field::Number,
  category: Field::Select.with_options(
    collection: Project::CATEGORIES.values,
    include_blank: true
  ),
  id: Field::Number,
  created_at: Field::DateTime,
  updated_at: Field::DateTime
}.freeze

FORM_ATTRIBUTES = %i[
  shortlister
  event
  project_allocation
  category
].freeze

我想过滤它而不是包含所有事件的下拉列表,以便它只包含 phase 属性设置为 'registrations_closed' 的事件。

我尝试通过扩展模型并添加另一个belongs_to 关系来做到这一点,即

class EventShortlister < ApplicationRecord
  belongs_to :event
  belongs_to :registration_closed_event,
             -> { where(phase: 'registrations_closed') },
             class_name: 'Event'

然后在仪表板ATTRIBUTE_TYPES 中包含这个新的下拉列表:

registration_closed_event: Field::BelongsTo.with_options(class_name: 'Event')

但这在加载页面时出错:

未定义的方法`registration_closed_event_id'

我应该改变什么来实现这种过滤,在哪里?

我想知道是否应该添加/编辑 scoped_resource 方法,但是在哪里?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-6 administrate


    【解决方案1】:

    是的,administrate 提供自定义范围,您也可以在以下选项中提供:

    registration_closed_event: Field::BelongsTo.with_options(class_name: 'Event', scope: -&gt; { Event.where(phase: 'registrations_closed') })

    您可以在 Customizing Fields -> Field::BelongsTo 部分下参考文档https://github.com/thoughtbot/administrate/blob/main/docs/customizing_dashboards.md 以获取更多信息,其中有一个自定义范围支持选项。希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2013-06-22
      • 2016-12-09
      • 2012-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-11
      • 2012-12-05
      • 1970-01-01
      相关资源
      最近更新 更多