我使用filterrific gem来过滤rails中的模型。
目前,我有三种型号,Video,Tagging,Tag
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),所以我在联接表tagging中使用tag_id进行筛选。
Tagging.rb
belongs_to :video
belongs_to :tagTag.rb
has_many :taggings
has_many :videos, through: :taggings目前,scope正在工作,但我不知道如何编写控制器和视图
控制器中:如何编写select_options方法?
视图:如何编写select方法?目前,我这样写,但没有用:
f.select(:by_tag, Tag.all, { include_blank: '- Any -' }, :class=>'form-control')发布于 2016-01-18 00:07:49
进入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助手
f.collection_select(:by_tag, Tag.all, :id, :name, prompt: '- Any -', class: 'form-control')第二个选项需要调整,这取决于控制器对空白选项所做的操作。
在APIDock ActionView::Helpers::FormOptionsHelper#select上有很好的例子。
https://stackoverflow.com/questions/34845150
复制相似问题