Ransack, acts-as-taggable-on and checkboxes in search form

805 views
Skip to first unread message

Sasha Chilly

unread,
May 27, 2013, 2:27:10 AM5/27/13
to rubyonra...@googlegroups.com
need some help on ransack filtering.

I have model Project, which has many tags through acts_as_taggable gem.
Project.rb:

    class Project < ActiveRecord::Base
      include PublicActivity::Common

      belongs_to :customer
      belongs_to :category
      has_many :attachments, :as => :attachable
      has_many :reports, :dependent => :destroy
      has_many :messages, :dependent => :destroy
      has_many :screens, :dependent => :destroy

      accepts_nested_attributes_for :attachments, :allow_destroy => true

      acts_as_taggable
    end

so the @project.tags - works in console just fine.

However, I have advanced search form with ransack gem on index page, and i need to filter projects depending on tags which are selected by user. So that only projects which have all of the selected tags were displayed.

Also a have categories, projects belong to categories. So the checkboxes for categories work as expected. But how can i do checkboxes for tags? Here's my form for search:

    = search_form_for @search do |f|
      - @categories.each do |category|
        = check_box_tag('q[category_id_eq_any][]', category.id)
          = category.name
      = f.text_field :budjet_gteq
      = f.text_field :budjet_lteq
      - @tags.each do |tag|
        = check_box_tag('q[tags_id_eq_all][]', tag.id)
        = tag.name

That's what i have in console after submiting the form (so i have no tags query at all, even though i checked two checkboxes):

    Processing by ProjectsController#index as HTML
      Parameters: {"utf8"=>"✓", "q"=>{"category_id_eq_any"=>["1"], "budjet_gteq"=>"100000", "budjet_lteq"=>"70000000"}}

Any help will be appreciated.

Sasha Chilly

unread,
May 27, 2013, 5:58:21 AM5/27/13
to rubyonra...@googlegroups.com
Edited my view, so now checkboxes for tags look like:
  - @tags.each do |tag|
    = check_box_tag("q[tags_id_eq_all][]", tag.id, {})
    = tag.name
So in console i can see such query:
Processing by ProjectsController#index as HTML

  Parameters: {"utf8"=>"✓", "q"=>{"budjet_gteq"=>"", "budjet_lteq"=>"", "tags_id_eq_all"=>["1", "2", "3", "4", "5", "6"]}}
So now, it passes all tags, even though i've selected only two of them. As the result it shows all projects, when i want to show only one, with two tags which i've checked.

Sasha Chilly

unread,
May 30, 2013, 4:35:15 AM5/30/13
to rubyonra...@googlegroups.com
Now i've played a bit and see that when in view i have:
  - @tags.each do |tag|
    = check_box_tag("q[tags_id_eq_all][]", tag.id, {})
    = tag.name
every checkbox in html is set to "checked" by default.
If i make it false, like this:
  - @tags.each do |tag|
    = check_box_tag("q[tags_id_eq_all][]", tag.id, false, {})
    = tag.name
Checkboxes are nit checked by default, but it doesn't send query with tags_ids array at all, even if i click on them.
Can't figure out, how can i get it working..

Sasha Chilly

unread,
May 30, 2013, 6:01:14 AM5/30/13
to rubyonra...@googlegroups.com
Sorry guys, problem was not in Ransack or checkboxes. it's all about html and styles, as checkboxes simply didn't get checked when i clicked them.
Fixing styles helped
Reply all
Reply to author
Forward
0 new messages