【问题标题】:Rails 5 - Ransack query with Simple Form grouping with OR combinatorRails 5 - 使用 OR 组合器进行简单表单分组的 Ransack 查询
【发布时间】:2021-12-13 00:14:07
【问题描述】:

在我的应用程序中,我有一个 JobAd 模型。有几个属性,我正在使用 Ransack,用 Simple Form 来查询这个模型。 对于某些属性,例如:title:category,我想使用默认组合器AND,但对于特定的一组属性,我想使用OR 组合器。这个属性是:

  • onsite_job(布尔型)
  • full_remote(布尔型)
  • partial_remote(布尔型)

到目前为止,我能够使表单与 AND 组合器一起使用。但我无法为这些字段创建组。如何使用 Ransack 和 Simple Form builder 来实现这一点。

在我看来,我有以下几点:

= search_form_for @q, url: backoffice_person_offer_suggestions_path(@person), remote: true, wrapper: :default20, builder: SimpleForm::FormBuilder, html: { method: :get } do |f|

  = f.input :category_id_in, as: :select, collection: -> { Category.all },label: 'Category', input_html: { class: 'ld-select-button', multiple: true }
  = f.input :experience_min_eq, collection: ProfileForm::EXPERIENCE_YEARS, label: 'Minimum Experience Years', input_html: { class: 'ld-select-button' }, include_blank: 'Any'

/ [...some other fields..]

  = f.input :onsite_job_true, as: :ld_boolean, label: 'Onsite', input_html: { include_hidden: false }
  = f.input :full_remote_true, as: :ld_boolean, label: 'Fully remote', input_html: { include_hidden: false }
  = f.input :partial_remote_true, as: :ld_boolean, label: 'Partially remote', input_html: { include_hidden: false }

/ [...some other fields..]

  = f.submit value: 'Search', class:'ld-button ld-blue-button ld-medium-button'

如何将这些属性分组到 OR 组合子下?


  • Ruby 2.6.6
  • Rails 5.2.5
  • PostgreSQL 10.18
  • 搜查 2.3.2
  • 简单表单 5.0.2

更新

如果我从我的项目中删除简单表单,我可以使用嵌套组生成适当的输入标签:

f.grouping_fields do |g|
  g.check_box :onsite_job_true
end

输出:

<input name="q[g][0][onsite_job_true]" type="hidden" value="0">
<input type="checkbox" value="1" name="q[g][0][onsite_job_true]" id="q_g_0_onsite_job_true">

但是当我添加简单表单时,如果我尝试如下代码:f.input_field :grouping_fieldsf.input :grouping_fields

我收到一个错误:

NoMethodError: undefined method `grouping_fields' for Ransack::Search<class: JobAd, base: Grouping <combinator: and>>:Ransack::Search

似乎 Simple Form 正在将 :grouping_fields 发送到 Ransack 实例@q

【问题讨论】:

标签: ruby postgresql ruby-on-rails-5 simple-form ransack


【解决方案1】:

问题在于 FormHelper 构建器设置为 builder: SimpleForm::FormBuilder

这就是为什么我无法做到f.grouping_fields

删除builder: SimpleForm::FormBuilder后,我能够做到:

= f.grouping_fields do |g|
  = g.input :m, as: :hidden, input_html: { value: 'or' }
  = g.input :onsite_job_true, as: :ld_boolean, label: 'Onsite', input_html: { include_hidden: false }
  = g.input :full_remote_true, as: :ld_boolean, label: 'Fully remote', input_html: { include_hidden: false }
  = g.input :partial_remote_true, as: :ld_boolean, label: 'Partially remote', input_html: { include_hidden: false }

它使用OR 组合器生成了适当的分组。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-21
    • 1970-01-01
    相关资源
    最近更新 更多