Rails Sunspot filter / facet issue with URL? Deleting old “get params”

▼魔方 西西 提交于 2020-01-05 04:45:07

问题


I'm trying to understand and set up Sunspot gem in my Rails 4.0 project. I'm trying to implement a better search in my open-source project, BTC-Stores, but I'm a bit confused about how to do that with Sunspot.

Currently, I have the following architecture (model):

Item:

# Relationship with Category
belongs_to :category 
accepts_nested_attributes_for :category

searchable do
    text :name, :description
    integer :category_id
    string  :sort_name do # why I have this here? I dont understand this code
          name.downcase.gsub(/^(an?|the)/, '')
    end
end

Controller:

@search = Item.search do
    fulltext params[:search] do
      boost_fields :name => 2.0
    end

    # With category
    facet :category_id
    with(:category_id, params[:category_id]) if params[:category_id].present?

    # Kaminari
    paginate :page => params[:page], :per_page => 8
end

@items = @search.results

# Here a quick fix to show Category Name, instead of Category ID to user.
@items_categories = []
@search.facet(:category_id).rows.each do |row|
    @items_categories << [Category.find_by_id(row.value), row.count] 
end 
@items_categories = @items_categories.sort_by { |e| e[0].name } 

View:

<% @items_categories.each do |category| %>
    <div class="country-item">
        <a href="#" class="country-row">
            <div class="country">
              <% if params[:category_id].blank? %>
                <%= link_to category[0].name, :category_id => category[0].id  %> (<%= category[1] %>)
              <% else %>
                <%= category[0].name %>(<%= link_to "remove", :category_id => nil %>)
              <% end %>
            </div>
        </a>
    </div>
 <% end %>

The problem:

When I search something, I have the desired results. Take a look at the categories and mainly at the URL:


Now, If i click in any of the listed categories, instead of add the category to "get" params, this is deleting the old params and then adding the category_id param.

Now my URL is http://localhost:3000/stores?category_id=6 instead of http://localhost:3000/stores?utf8=%E2%9C%93&search=bitcoin&category_id=6. Look:

So, what can I doing wrong? And another thing, if you see issues in my code and things that can be better done, please tell me. I read all Sunspot documentation and RailsCast by Ryan Bates, but I don't understand how I can do things by the "right way".


回答1:


Taking a hint from this answer

Instead of

<%= link_to category[0].name, :category_id => category[0].id  %> (<%= category[1] %>)

Try using

<%= link_to category[0].name, request.parameters.merge({:category_id => category[0].id})  %> (<%= category[1] %>)

This would append the category ID to the existing get parameters.

However this has a downside too - Since you are using Facets, drilling down would keep appending parameters, and you could end up doing an AND by default. i.e. something like

http://localhost:3000/stores?utf8=%E2%9C%93&search=bitcoin&category_id=6&category_id=7

So unless you want the drill down feature by default, you might want to tweak the params merging logic.



来源:https://stackoverflow.com/questions/20961485/rails-sunspot-filter-facet-issue-with-url-deleting-old-get-params

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!