【问题标题】:How to use filterrific gem in rails?如何在 Rails 中使用 filterrific gem?
【发布时间】:2016-01-17 23:37:30
【问题描述】:

我已经使用filterrific gem 来过滤 rails 中的模型。

目前,我有三个模型,VideoTaggingTag

Video.rb

has_one :tagging
has_one :tag, through: :tagging

scope :by_tag, -> (tag_id) {joins(:tagging).where(taggings: {tag_id: tag_id})}

因为tag.name很难做过滤(见StackOverflow),所以我在join table tagging中使用tag_id做过滤。

标记.rb

belongs_to :video
belongs_to :tag

标签.rb

has_many :taggings
has_many :videos, through: :taggings

目前scope正在工作,但我不知道如何编写controllerview

在控制器中:如何编写select_options 方法?

观点:select方法怎么写?目前,我这样写,但不工作:

f.select(:by_tag, Tag.all, { include_blank: '- Any -' }, :class=>'form-control')

【问题讨论】:

    标签: ruby-on-rails filterrific


    【解决方案1】:

    转到select 标签助手的选择选项需要看起来像一对[ [ name, id ], [ name, id ] ... ] 数组。试试这样的:

    f.select(:by_tag, Tag.all.map {|tag| [tag.name, tag.id]}, { include_blank: '- Any -' }, :class=>'form-control')
    

    或者为了让事情更干净,你可以使用 rails collection_select helper 类似的东西

    f.collection_select(:by_tag, Tag.all, :id, :name, prompt: '- Any -', class: 'form-control')
    

    第二个需要根据控制器对空白选项的操作进行调整。

    APIDock ActionView::Helpers::FormOptionsHelper#select上有很好的例子。

    【讨论】:

      猜你喜欢
      • 2011-06-19
      • 1970-01-01
      • 1970-01-01
      • 2015-03-30
      • 1970-01-01
      • 2022-10-20
      • 1970-01-01
      • 1970-01-01
      • 2017-07-24
      相关资源
      最近更新 更多