【问题标题】:Rails Sunspot filter / facet issue with URL? Deleting old "get params"URL的Rails Sunspot过滤器/构面问题?删除旧的“获取参数”
【发布时间】:2014-01-24 12:53:01
【问题描述】:

我试图在我的 Rails 4.0 项目中理解和设置Sunspot gem。我正在尝试在我的开源项目BTC-Stores 中实现更好的搜索,但我对如何使用 Sunspot 执行此操作有点困惑。

目前,我有以下架构(模型):

项目:

# 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

控制器:

@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 } 

查看:

<% @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 %>

问题:

当我搜索某些内容时,我会得到想要的结果。看一下分类,主要看网址:


现在,如果我单击任何列出的类别,而不是将类别添加到“获取”参数,这是删除旧参数,然后添加 category_id 参数。

现在我的网址是http://localhost:3000/stores?category_id=6,而不是http://localhost:3000/stores?utf8=%E2%9C%93&amp;search=bitcoin&amp;category_id=6。看:

那么,我做错了什么?还有一件事,如果您在我的代码中发现问题以及可以做得更好的事情,请告诉我。我阅读了所有Sunspot documentationRailsCast by Ryan Bates,但我不明白如何以“正确的方式”做事。

【问题讨论】:

    标签: ruby-on-rails solr sunspot


    【解决方案1】:

    得到this answer的提示

    代替

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

    尝试使用

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

    这会将类别 ID 附加到现有的 get 参数中。

    但是,这也有一个缺点 - 由于您使用的是 Facets,因此向下钻取将继续附加参数,并且您最终可能会默认执行 AND。即类似

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

    因此,除非您希望默认使用向下钻取功能,否则您可能需要调整 params 合并逻辑。

    【讨论】:

    • 谢谢,这行得通!但是你确定这是更好的方法吗?为什么瑞安贝茨不使用它并工作? (如果不想,您不需要回答这两个问题,但下一个需要 :P )。点击我选择的类别后,我被重定向,在这个新页面中,当我点击“删除”(&lt;%= link_to "remove", :category_id =&gt; nil %&gt;,我的浏览器转到localhost:3000/stores。我如何“合并”我的参数并删除一个其中(category_id,在这个 cade)?
    • 哇,我自己找到了答案。只需&lt;%= link_to "remove", request.parameters.merge({:category_id =&gt; nil}) %&gt;。再次感谢。
    • 最好的答案已经是你的了,但是你能帮我回答这两个问题吗? “但你确定这是更好的方法吗?为什么 Ryan Bates 不使用它并工作?”
    • @FernandoPaladini- 我唯一的猜测是 Ryan 的 example 有一个单一的场景:)。我还没有遇到更好的东西,是的,它看起来很奇怪,但我只是添加了一个帮助程序以从 get 请求到达 params 并使用它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-03
    • 1970-01-01
    • 2011-07-10
    • 2011-12-17
    • 2016-05-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多