【问题标题】:Ruby on Rails 4: Searchkick displaying all items instead of searched resultsRuby on Rails 4:Searchkick 显示所有项目而不是搜索结果
【发布时间】:2016-06-27 04:34:44
【问题描述】:

我有一个Forum 那个has_many :forum_threadshas_many :forum_posts, through: :forum_threads。所以基本上我的论坛有自己的论坛主题,这些主题与特定的论坛主题相关。我添加了 Searchkick 来处理搜索表单。 Searchkick 适用于我的 Forum 表,但不适用于我的 ForumThread 表。

没有错误,它只是渲染出所有的论坛线程,而不是渲染给定表单的查询。

Searchkick GitHub

这是我的文件:

forum.rb

class Forum < ActiveRecord::Base

 accepts_nested_attributes_for :forum_threads
 has_many :forum_posts, through: :forum_threads
 accepts_nested_attributes_for :forum_posts

 searchkick text_start: [:title], suggest: [:title], autocomplete: [:title]

    def search_data         
            {
                title: title                    
            }
    end

end

forums_controller.rb

class ForumsController < ApplicationController

 def index
    query = params[:q] || "*"
    @forums = Forum.search(query, suggest: true, fields: [:title],
                        boost_where: {specific: :exact})
 end

end

观点/论坛/index.html.erb

<%= form_tag forums_path, method: :get do |f| %>
  <%= text_field_tag :q, nil, class: 'form-control', placeholder: 'Search...' %>
<% end %>

  <% if @forums.suggestions.any? %>
    <p class="lead">
        <em>Did you mean: <strong><%= @forums.suggestions.first %></strong>?</em>
    </p>
  <% end %>

forum_thread.rb

class ForumThread < ActiveRecord::Base

 belongs_to :user, counter_cache: true
 belongs_to :forum, counter_cache: true, touch: true
 has_many :forum_posts, dependent: :destroy
 accepts_nested_attributes_for :forum_posts

 validates :subject, presence: true
 validates_associated :forum_posts

 searchkick text_start: [:subject], suggest: [:subject], autocomplete: [:subject]

    def search_data         
            {   
                subject: subject,   
                description: description                            
            }
    end

end

forum_threads_controller.rb

class ForumsController < ApplicationController

 def index
    query = params[:q].presence || "*"
        @forum_threads = @forum.forum_threads.search(query, suggest: true, fields: [:subject, :description])
 end

end

views/forum_threads/index.html.erb

<%= form_tag forum_forum_threads_path(<!-- something here? -->), method: :get do %>
    <%= text_field_tag :q, nil, class: 'form-control', placeholder: 'Search...' %>
<% end %>


 <% if @forum_threads.suggestions.any? %>
    <p class="lead"><em>Did you mean: <%= @forum_threads.suggestions.first %>?</em></p>
 <% end %>

routes.rb

Rails.application.routes.draw do

 resources :forums do
  resources :forum_threads do
    resources :forum_posts do
      member do
        put 'like', to: 'forum_posts#upvote'
        put 'dislike', to: 'forum_posts#downvote'
      end 
     end
    end
   end

end

【问题讨论】:

    标签: ruby-on-rails-4 search elasticsearch searchkick


    【解决方案1】:

    我刚刚为在视图中使用的建议创建了一个变量。

    forum_threads_controller.rb

    def index   
        query = params[:q].presence || "*"
            @forum_threads = ForumThread.search(query,
                where: { forum_id: @forum.id },
             fields: [:subject, :description], 
                                    boost_where: {specific: :exact}, suggest: true, highlight: true)
    
        @forum_threads_suggestions = []
            if @forum_threads.empty? && @forum_threads.suggestions.any?
            @forum_threads_suggestions = ForumThread.search(@forum_threads.suggestions.first, fields: [:subject], suggest: true,
                        boost_where: {specific: :exact}, highlight: true)
            end
    end
    

    index.html.erb

    <%= form_tag forum_forum_threads_path, method: :get do %>
                    <%= text_field_tag :q, nil, class: 'form-control', placeholder: 'Search...' %>
                <% end %>
    
                <% if @forum_threads.suggestions.any? %>
                    <p class="lead">
                        <em>
                            Did you mean: <strong><%= @forum_threads.suggestions.first %></strong>?
                        </em>
                    </p>
                <% end %>
    
        </div>
        <!-- END SEARCH FORM -->    
    
                                        <!-- SHOW RESULTS AND SUGGESTIONS -->
            <p class="light-grey"><em>
                <%= pluralize @forum_threads.results.size, 'match' if params[:q].present? %>
                <%= pluralize @forum_threads.suggestions.size, 'suggestion' if @forum_threads.suggestions.any? %>
            </em></p>
                                        <!-- END RESULTS AND SUGGESTIONS -->
    
                                        <!-- SHOW FORUM THREADS -->
        <% (@forum_threads.presence || @forum_threads_suggestions).each do |forum_thread| %>
    

    我写了一个声明来显示所有论坛主题或建议。

    forum_thread.rb)

    searchkick fields: [:subject], suggest: ['subject'], highlight: [:subject]
    
    def search_data
        {
            forum_id: forum.id,
            forum_thread: @forum_thread,
            subject: subject
        }
    end
    

    我为论坛模型做了同样的事情。我不确定这是否是最好的方法,所以如果其他人有东西,请随时贡献。

    【讨论】:

      猜你喜欢
      • 2016-04-07
      • 2014-06-19
      • 2016-03-24
      • 1970-01-01
      • 2012-07-23
      • 2013-12-23
      • 1970-01-01
      • 2016-03-25
      • 1970-01-01
      相关资源
      最近更新 更多