LoginSignup
3
3

More than 5 years have passed since last update.

ActsAsTaggableOnのタグをチェックボックスで複数選択して検索 w/ Ransack

Last updated at Posted at 2018-03-05

ActsAsTaggableOnのタグをチェックボックスで複数選択してRansackで検索(主にViewだけ)

comments_controller.rb
  ...

  def search
    @tags = ActsAsTaggableOn::Tag.all
    @q = Comment.ransack(params[:q])
  end

  ...

Viewはラベルをクリックでも選択できるように

_search_form.html.haml
= search_form_for(@q, url:result_path) do |f|
  .form-group
    - @tags.each do |t|
      = f.check_box :tags_id_in, { multiple: true, :checked => t.id, include_hidden: false }, t.id
      %label.badge.badge-primary{:for => "q_tags_id_in_#{t.id}"}= t.name

  .form-group
    = f.submit "Search", class: "btn btn-lg btn-primary"

search_form.sass
input[type="checkbox"]
    display: none
    &:checked + .badge-primary
      font-weight: bold
      font-size: 2rem
      background-color: #dc3545

チェックボックス本体はdisplay: noneで隠しておく。
あとはお好みでスタイル当てればOK

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3