在我的 Rails Controller 中,我写了
@all = Sunspot.search(Car, Article, Organization) do
fulltext params[:search]
end
@all_res = @all.results
然后我有这个数据:
[#<Car id: 25, vehicle_manufacturer_id: 2, vehicle_model_id: 2, price: 0, year: 1900, eng_capacity: 1, fuel_type_id: 2, gearbox_id: 2, wheeldrive_id: 2, is_not_rus: false, color_id: 3, vehicle_registration_id: 2, condition_id: 1, body_type_id: 3, is_exchangeable: false, is_not_sell: nil, is_right_wheel: false, is_without_run_in_russia: false, is_gt: false, is_new: false, is_hybrid: nil, has_ac: false, has_leather: false, has_alloy_wheels: false, has_el_packet: false, has_signalization: false, user_id: 3, description: "Кароче типо крутая тачка", created_at: "2014-03-18 18:20:51", updated_at: "2014-03-18 20:29:32", vehicle_mileage: 0, contact_name: "df sdf ", city_id: 3, telephone: "123123", is_vip: nil>, #<Article id: 6, meta_description: "dfsdf", meta_keywords: "dssd", title: "sdffsdf", tags: "1", intro_text: "sdfsdf", content: "sfsdf sdf sdf ds \r\nкрутая", author_id: 1, articles_type_id: nil, published: true, publishing_start: "2014-02-25 18:02:00", publishing_end: "2014-02-25 18:02:00", is_advert: true, created_at: "2014-02-25 18:03:07", updated_at: "2014-02-25 18:03:07", article_image_file_name: nil, article_image_content_type: nil, article_image_file_size: nil, article_image_updated_at: nil, rostov: nil, krasnodar: nil, stavropol: nil>]
但是我怎么能把它展示得漂亮呢?
例如,如果是汽车,则查看某个字段,如果查看搜索结果中的文章不是其他字段?也可能是真实的显示 - 这个词是在哪里找到的?
最佳答案
试试这个,它可能适合你的情况
@all.results.each do |result|
case result.class.name
when 'Car'
<%= result.vehicle_manufacturer_id %>
<%= result.vehicle_model_id %>
when 'Article'
<%= result.meta_description %>
<%= result.meta_keywords %>
end
end
关于ruby-on-rails - 如何显示 Rails SOLR 多模式搜索的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22846293/