Ken Beegle

Joined

20 Experience
0 Lessons Completed
0 Questions Solved

Activity

There was a mistake in my code. I had abstracted it out into a partial and when I rewrote the partial to match Chris's example, I wrote it wrong. My original example was looking into params["bucket"] and it should have been params["brand"].

Starting from the beginning, the third argument of the check_box_tag determines whether the check_box is selected or not. It'll take a true/false value, but you can substitute a nil value for the false. Orignally, I began with params["brand"].include? bucket["key"]. This will return a true/false value but will fail when the params["brand"] is not set, for example when first hitting the search page and no params are set because there is no .include? method on a nil class.

From there, I integrated the .try method, so if the params["brand"] supports the .include? method, it'll use it and return a true/false value. If the params["brand"] method doesn't support .include? then it'll return a nil value to the check_box_tag and the check box will not be selected.

The final code should read as: params["brand"].try(:include?, bucket["key"]). Note, on the .try method, the first argument is the method I'm calling and the second argument is the value I'm passing to the .include? method.

https://api.rubyonrails.org/v4.2/classes/Object.html#method-i-try

This has been very helpful for a project I'm working on. Thank you. On mine, I wanted to add checkboxes to be able to select multiple keys. For anyone else looking to do the same, the solution I came up with is:

<%= form_with(method: :get, local: true, html: { class: 'query'}) do |form| %>
  <h6>Brand</h6>
  <% @televisions.aggs["brand"]["buckets"].sort_by{ |b| b["key"] }.each do |bucket| %>
    <%= check_box_tag "brand[]",
                      bucket["key"],
                      params["brand"].try(:include?, bucket["key"]),
                      id: "brand_#{bucket["key"].to_s.parameterize.underscore}" %>
    <%= label_tag "brand_#{bucket["key"].to_s.parameterize.underscore}", bucket["key"].to_s %>
    (<%= bucket["doc_count"] %>)
    <br>
  <% end %>

  <%= form.submit %>
<% end %>

In the check_box_tag, I added '[]' to the name so "brand[]" establishes an Array. Searchkick can use this to filter for any of the values in the array without changing anything Chris's controller code.

For the id value in the check_box_tag and for value in the label_tag, I used .to_s.parameterize.underscore. This removes any spaces that might be in the bucket key and then standardizes it to my other code with underscores.

In my example I added a submit button but I'll be removing that in production and adding a Stimulus controller to submit the form on click or do a little AJAX to just load the results.

I hope this helps anyone else diving into it and I'm definitely open to any suggestions to make the code better.

edit: I changed the code that states whether the check box should be selected to use the .try method. Before, an error would be raised if no values were selected. This way, it just provides a nil value and doesn't check that box if the params do not exist.