我正在使用 sunspot gem 并想要实现这样的搜索表单
我有两篇示范文章和书籍
这是我的文章模型
class Article < ActiveRecord::Base
searchable do
text :title
text :content
end
这是我的书籍模型
class Book < ActiveRecord::Base
searchable do
text :title
text :description
end
我想知道如何实现这样的搜索表单,用户可以选择他们想要搜索的书籍或文章的内容
最佳答案
试试这个
http://railscasts.com/episodes/278-search-with-sunspot
gem 文件
gem 'sunspot_rails'
重击
bundle
rails g sunspot_rails:install
rake sunspot:solr:start
rake sunspot:reindex
模型/article.rb
searchable do
text :name, :boost => 5
text :content, :publish_month
text :comments do
comments.map(&:content)
end
time :published_at
string :publish_month
end
def publish_month
published_at.strftime("%B %Y")
end
文章_controller.rb
def index
@search = Article.search do
fulltext params[:search]
with(:published_at).less_than(Time.zone.now)
facet(:publish_month)
with(:publish_month, params[:month]) if params[:month].present?
end
@articles = @search.results
end
文章/index.html.erb
<%= form_tag articles_path, :method => :get do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
<div id="facets">
<h3>Published</h3>
<ul>
<% for row in @search.facet(:publish_month).rows %>
<li>
<% if params[:month].blank? %>
<%= link_to row.value, :month => row.value %> (<%= row.count %>)
<% else %>
<strong><%= row.value %></strong> (<%= link_to "remove", :month => nil %>)
<% end %>
</li>
<% end %>
</ul>
</div>
关于ruby-on-rails - rails : search in multiple model with select option,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22948282/