【发布时间】:2014-06-04 21:02:41
【问题描述】:
我在实施 Sunspot 时遇到了一些问题!在过去的 2 天里,我一直在尝试让它工作,但我似乎找不到任何解决方案(尽管它无疑一直在盯着我看!)。
我希望用户能够搜索汽车制造商。创建车辆时,会通过关联为其分配制造商(如下所示)。现在..我如何允许用户在搜索字段中输入(或者更好的是所有制造商的 collection_select)并且它会过滤掉结果?
在理想的世界中,我希望将其用作我的搜索:
<%= simple_form_for :search, url: vehicles_path , :method => :get do |f| %>
<%= select_tag :manufacturer, options_from_collection_for_select(Manufacturer.all, :id, :name) %>
<%= submit_tag "Search", :name => nil %>
<% end %>
但是很遗憾,我无法在我的 Vehicle 模型中找到解决方案。
如果这对你有任何意义,我希望你能引导我走向正确的方向!
非常感谢!
class Vehicle < ActiveRecord::Base
belongs_to :manufacturer
searchable do
text :name, :registration
integer :manufacturer_id
end
def to_s
self.name
end
end
然后
class VehiclesController < ApplicationController
before_action :set_vehicle, only: [:show, :edit, :update, :destroy]
def index
@manufacturer = Manufacturer.all
@search = Vehicle.search do
fulltext params[:search]
end
@vehicles = @search.results
end
end
和
class Manufacturer < ActiveRecord::Base
has_many :vehicles
searchable do
text :name
end
def to_s
self.name
end
end
车辆索引.html.erb
<%= form_tag vehicles_path, :method => :get do %>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
<% end %>
还有我的车辆形态
<%= simple_nested_form_for @vehicle, :html=>{:multipart => true } do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :name %>
<%= f.association :manufacturer, include_blank: false %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
为了方便起见,我删除了示例中大部分不必要的内容。
为了一笑而过,你可以看看我失败的尝试!
我也跑了这么多 rake sunspot:reindex 这太疯狂了。
#integer :manufacturer_id, :multiple => true
#integer :manufacturer_id, :references => Manufacturer, :multiple => true
#integer :manufacturer_id, :references => Manufacturer, :multiple => true do
# manufacturers.map {|manufacturer| manufacturer.manufacturer_name}
#end
#text :manufacturer_names do |manufacturer|
# manufacturer.name { |manufacturer| manufacturer.name }
#end
#integer :manufacturer_id {|manufacturer| manufacturer.name }
#string :manufacturer_name do
# manufacturer.name { |manufacturer| manufacturer.name }
#end
#text :manufacturers do
# manufacturers.map { |manufacturer| manufacturer.name }
#end
#integer :manufacturer_id, :stored => true
【问题讨论】:
标签: ruby-on-rails sunspot sunspot-rails sunspot-solr