[英]How to use filterrific gem in rails?
I have used filterrific
gem to filter the model in rails. 我使用了filterrific
宝石来过滤轨道中的模型。
Currently, I have three model, Video
, Tagging
, Tag
目前,我有三种模型, Video
, Tagging
, Tag
Video.rb Video.rb
has_one :tagging
has_one :tag, through: :tagging
scope :by_tag, -> (tag_id) {joins(:tagging).where(taggings: {tag_id: tag_id})}
Because it's hard to use tag.name
to do the filter(see StackOverflow ), so I use tag_id
in join table tagging
to do the filter. 因为很难使用tag.name
进行过滤(请参见StackOverflow ),所以我在tag_id
表tagging
使用tag_id
进行过滤。
Tagging.rb Tagging.rb
belongs_to :video
belongs_to :tag
Tag.rb Tag.rb
has_many :taggings
has_many :videos, through: :taggings
Currently, the scope
is working, but I don't know how to write controller and view 目前, scope
正在运行,但我不知道如何编写控制器和视图
In controller: How to write select_options
method? 在控制器中:如何编写select_options
方法?
In view: How to write select
method? 鉴于:如何编写select
方法? Currently, I write like this, but not working: 目前,我这样写,但是不起作用:
f.select(:by_tag, Tag.all, { include_blank: '- Any -' }, :class=>'form-control')
Your select options that go to the select
tag helper need to look like an array of pairs [ [ name, id ], [ name, id ] ... ]
. select
标签帮助器的选择选项需要看起来像是成对的数组[ [ name, id ], [ name, id ] ... ]
。 Try something like this: 尝试这样的事情:
f.select(:by_tag, Tag.all.map {|tag| [tag.name, tag.id]}, { include_blank: '- Any -' }, :class=>'form-control')
Or to keep things even cleaner, you could use the rails collection_select
helper with something like 为了使环境更清洁,您可以将rails collection_select
辅助程序与类似
f.collection_select(:by_tag, Tag.all, :id, :name, prompt: '- Any -', class: 'form-control')
The second one many need tweaking depending on what your controller is doing with the blank option. 第二个需要根据控制器使用空白选项进行的操作进行调整。
There are good examples on APIDock ActionView::Helpers::FormOptionsHelper#select . APIDock ActionView :: Helpers :: FormOptionsHelper#select上有很好的示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.