Ruby on rails Rails-太阳黑子Solr搜索-显示post三次
我正在制作一个需要使用搜索表单的Rails应用程序。 在我的应用程序中,用户可以创建一个帖子“广告”,然后指定他希望它是小型、中型还是特色大小的帖子 在我尝试将Sunspot Solr搜索融入我的应用程序之前,这一切都很正常。 无论何时我创建一篇文章,无论是小文章、中文章还是特色文章,只要它只创建所选内容,就会创建各种大小的文章 广告控制器Ruby on rails Rails-太阳黑子Solr搜索-显示post三次,ruby-on-rails,search,solr,size,sunspot,Ruby On Rails,Search,Solr,Size,Sunspot,我正在制作一个需要使用搜索表单的Rails应用程序。 在我的应用程序中,用户可以创建一个帖子“广告”,然后指定他希望它是小型、中型还是特色大小的帖子 在我尝试将Sunspot Solr搜索融入我的应用程序之前,这一切都很正常。 无论何时我创建一篇文章,无论是小文章、中文章还是特色文章,只要它只创建所选内容,就会创建各种大小的文章 广告控制器 class AdsController < ApplicationController before_action :set_ad, only:
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
类AdsController“medium”).search do
全文参数[:搜索]
结束
@search_featured=Ad.where(:size=>“featured”)。搜索do
全文参数[:搜索]
结束
@搜索\u small=Ad.where(:size=>“small”)。搜索do
全文参数[:搜索]
结束
@ads=Ad.all
@ads\u small=@search\u small.results
@ads_medium=@search_medium.results
@ads\u featured=@search\u featured.results
结束
#GET/ads/1
#GET/ads/1.json
def秀
结束
#获取/广告/新
def新
@ad=ad.new
结束
#获取/ads/1/edit
定义编辑
结束
#张贴/广告
#POST/ads.json
def创建
@ad=ad.new(ad_参数)
@ad.user\u id=当前用户id
回应待办事项|格式|
如果@ad.save
format.html{将_重定向到@ad,注意:'ad已成功创建。}
format.json{呈现操作:'show',状态::已创建,位置:@ad}
其他的
format.html{呈现操作:'new'}
format.json{render json:@ad.errors,status::unprocessable_entity}
结束
结束
结束
#补丁/PUT/ads/1
#PATCH/PUT/ads/1.json
def更新
回应待办事项|格式|
如果@ad.update(ad_参数)
format.html{将_重定向到@ad,注意:'ad已成功更新。}
format.json{head:no_content}
其他的
format.html{呈现操作:“编辑”}
format.json{render json:@ad.errors,status::unprocessable_entity}
结束
结束
结束
#删除/ads/1
#删除/ads/1.json
def销毁
@自毁
回应待办事项|格式|
format.html{redirect_to ads_url}
format.json{head:no_content}
结束
结束
私有的
#使用回调在操作之间共享公共设置或约束。
def set_ad
@ad=ad.find(参数[:id])
结束
#永远不要相信来自恐怖网络的参数,只允许白名单通过。
def ad_参数
参数require(:ad).permit(:title,:url,:preview,:location,:size,:info)
结束
结束
我的索引视图
<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 %>
.ads_链接{
字体系列:NexaBold、Helvetica、Arial、无衬线字体;
颜色:#e74c3c!重要;
}
:get-do%>
零%>
“标题”%>
:delete,:class=>“delete”%>
“标题”%>
:delete,:class=>“delete”%>
:删除%>
我的模型文件
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
class-Ad“missing.jpg”、:style=>{:medium=>“125x125^”、“featured=>“250x250^”、“showpg=>“400x400^”}、:convert\u选项=>{:medium=>“-重心-范围125x125”、:featured=>“-重心-范围250x250”、:showpg=>“-重心-范围400x400”}
验证:标题,长度:{最大值:35}
验证:url,长度:{最大值:40}
可搜索do
文本:标题,:信息
结束
结束
1.你有没有访问8983端口,看看是否真的有3个广告得到索引。2.模型文件看起来像什么?您是如何定义可搜索方法的。嘿,我已经用我的模型编辑了这个问题。:)尽管如此,我认为您应该首先使用端口8983检查sunspot本地服务器,看看是否确实有3条相同id的记录。我建议您将大小字段添加到可搜索的定义中,并在不使用where方法调用的情况下进行搜索。因为这是我第一次使用sunspot,我不太确定如何从我的本地服务器找到记录。对不起,我刚弄明白,不,我只有一条记录,但不知怎么的,它被显示了三次。