Adding to params in a url and removing them if they already exist

I am trying to add facet navigation to my website, without the use of Gems.

I have this in my view

<% facets.each do |keyword, values| %>
    <h4 class="facet-heading">
      <%= keyword.gsub('_', ' ').titleize %>
    </h4>
    <ul class="facets">
      <% values.each do |value| %>
        <li>
          <%= link_to controller: "catalog13",
            action: "mgroup", id: params[:id].gsub('_', ' '),
            lv2: params[:lv2].gsub('_', ' '),
            pagetype: params[:pagetype], keyword: keyword,
            value: value.value do %>
              <%= image_tag "m13/checkbox_unselected.jpg", size: "12x12" %>
              <%=   value.value %>
          <% end %>
        </li>
      <% end %>
    </ul>
  <% end %>

and this in my controller

def facets
    if params[:id] && params[:lv2]
      catalogs = current_user.customer.catalog_list.split(",")

      groupcodes = Matrixindex
        .where(
          catalog_id: catalogs,
          idesclv1: params[:id].gsub('_', ' '),
          idesclv2: params[:lv2].gsub('_', ' '),
          pagetype: "C"
        )
        .group(:groupcode)
        .map{|product| product.groupcode}

      pmets = Matrixpmet.select("keyword, value").where(groupCode: groupcodes).group(:value)
      pmets = pmets.group_by {|pmets| pmets.keyword}
      pmets.delete("Description")
      pmets.delete("Manufact. Part No.")
      @facets = pmets
    end
  end
  helper_method :facets

I am trying to make it so when the link is clicked the image changes to a ticked checkbox and the keyword and value is added to the url, if the link is clicked again I would like it so it removes from the urls params and the image changes to an empty check box. I am also trying to get it so if you click more than one link it adds to the url’s params too but everything I am trying is overwriting the already exisitng keyword and value params in the url.

I am trying to get it to work like the Amazon one does.

Any help is greatly appreciated.

I’d start by breaking this problem up into smaller parts. We usually advocate working from the outside (the view) in (through the controller, to the model), but in this case since you’re unsure of where you’re heading, it might help you to start from the model and work outwards.

  1. Can you add a class method to your product model that returns filtered results? Think carefully about the cases you want to support (multiple facets, multiple values for the same facet, etc.) and the kind of input this method should accept; is it a Hash, an Array, what’s most helpful for building the query you need?
  2. Now you know what data you need to pass to your model to load the relevant products, how do you want that to be passed to the controller? Can you pass the structure your model needs as part of the params hash, or do you need to do some additional work in the controller?
  3. Now you know what your controller wants, how would you build a URL in your view that passes those parameters?