简体   繁体   English

Rails-太阳黑子Solr搜索-显示帖子三遍

[英]Rails - Sunspot Solr Search - Displays post three times

i'm making a Rails app that requires the use of a search form. 我正在制作需要使用搜索表单的Rails应用程序。 In my app, a user can create a post, 'Ad', and then specify if he wants it to be a small, medium or featured sized post. 在我的应用中,用户可以创建一个帖子“广告”,然后指定他是否希望它是小型,中型或精选尺寸的帖子。

This was working fine until I attempted to incorporate Sunspot Solr Search into my app. 直到我尝试将Sunspot Solr Search合并到我的应用中之前,此方法一直工作良好。 No when ever I create a post, be it small, medium or featured, it creates on of every size whenever it should only create the selected one. 不管我是创建小型,中型还是特色的帖子,只要它只创建所选的帖子,它就会创建各种尺寸的帖子。

Ad Controller 广告控制器

class AdsController < ApplicationController
  before_action :set_ad, only: [:show, :edit, :update, :destroy]

  # GET /ads
  # GET /ads.json
  def index
    @search_medium = Ad.where(:size => "medium").search do 
      fulltext params[:search]
    end

    @search_featured = Ad.where(:size => "featured").search do 
      fulltext params[:search]
    end

    @search_small = Ad.where(:size => "small").search do 
      fulltext params[:search]
    end


    @ads = Ad.all
    @ads_small = @search_small.results
    @ads_medium = @search_medium.results
    @ads_featured = @search_featured.results

  end

  # GET /ads/1
  # GET /ads/1.json
  def show
  end

  # GET /ads/new
  def new
    @ad = Ad.new
  end

  # GET /ads/1/edit
  def edit
  end

  # POST /ads
  # POST /ads.json
  def create
    @ad = Ad.new(ad_params)
    @ad.user_id = current_user.id

    respond_to do |format|
      if @ad.save
        format.html { redirect_to @ad, notice: 'Ad was successfully created.' }
        format.json { render action: 'show', status: :created, location: @ad }
      else
        format.html { render action: 'new' }
        format.json { render json: @ad.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /ads/1
  # PATCH/PUT /ads/1.json
  def update
    respond_to do |format|
      if @ad.update(ad_params)
        format.html { redirect_to @ad, notice: 'Ad was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @ad.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /ads/1
  # DELETE /ads/1.json
  def destroy
      @ad.destroy
      respond_to do |format|
        format.html { redirect_to ads_url }
        format.json { head :no_content }
      end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_ad
      @ad = Ad.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def ad_params
      params.require(:ad).permit(:title, :url, :preview, :location, :size, :info)
    end
end

My Index View 我的索引视图

<style type="text/css">
.ads_link {
  font-family: NexaBold, Helvetica, Arial, sans-serif;
  color: #e74c3c!important;
}
</style>

<%= form_tag ads_path, :method => :get do %>
  <p>
    <%= text_field_tag :search, params[:search] %>
    <%= submit_tag "Search", :name => nil %>
<% end %>

<% @ads_featured.each do |ad| %>
  <div class="adspace_grid_f">
    <div class="pic_sec_f">
      <%= image_tag ad.preview.url(:featured) %>
    </div>
    <div class="text_info_f">
      <span class="bold"><%= link_to ad.title, ad, :class => "title" %></span></br>
      <p><%= truncate(ad.info, length: 90) %></p>
      <span class="bold"><%= ad.location %></span></br>
      <span class="bold"><%= ad.url %></span>

    </div>
    <% if can? :destroy, ad %>
      <%= link_to 'Delete', ad, :method => :delete, :class => "delete" %> 
    <% end %>
  </div> 
<% end %>




<% @ads_medium.each do |ad| %>
  <div class="adspace_grid">
    <div class="pic_sec">
      <%= image_tag ad.preview.url(:medium) %>
    </div>
    <div class="text_info">
      <span class="bold"><%= link_to ad.title, ad, :class => "title" %></span></br>
      <p><%= truncate(ad.info, length: 60) %></p>
      <span class="bold"><%= ad.location %></span></br>
      <span class="bold"><%= ad.url %></span>

    </div>
    <% if can? :destroy, ad %>
      <%= link_to 'Delete', ad, :method => :delete, :class => "delete" %> 
    <% end %>
  </div> 
<% end %>

<% @ads_small.each do |ad| %>
  <div class="adspace_grid">
    <div class="pic_sec">
      <%= image_tag ad.preview.url(:medium) %>
    </div>
    <div class="text_info">
      <span class="bold"><%= ad.title %></span></br>
      <p><%= truncate(ad.info, length: 30) %></p>
      <div class="right-text">
        <span class="bold"><%= ad.location %></span></br>
        <span class="bold"><%= ad.url %></span>
      </div>
    </div>
    <% if can? :destroy, ad %>
      <%= link_to 'Delete', ad, :method => :delete %> 
    <% end %>
  </div> 
<% end %>

My Model File 我的模型文件

class Ad < ActiveRecord::Base

    attr_accessible :title, :url, :preview, :size, :location, :info
    belongs_to :user
  has_attached_file :preview, :default_url => "missing.jpg", :styles => { :medium => "125x125^", :featured => "250x250^", :showpg => "400x400^" }, :convert_options => {:medium => "-gravity center -extent 125x125", :featured => "-gravity center -extent 250x250", :showpg => "-gravity center -extent 400x400"}

  validates :title, length: { maximum: 35 }
  validates :url, length: { maximum: 40 }

  searchable do
    text :title, :info
  end
end
  1. the local sunspot server is running on the port 8983 by default, and it provide a web admin there. 默认情况下,本地sunspot服务器在端口8983上运行,并且在那里提供了Web管理员。 You can do a search by entering the query as ":", and this will return all the indexed record inside sunspot. 您可以通过将查询输入为“:”来进行搜索,这将返回黑子中的所有索引记录。

  2. I suggest you add the size field into the searchable definition and do the search without the where method call. 建议您将size字段添加到可搜索的定义中,并在不使用where方法调用的情况下进行搜索。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM