如何在Rails網站里加搜索功能===>
插件acts_as_ferret(http://github.com/jkraemer/acts_as_ferret ),詳細用法可參考下面例子...
(1)基礎篇:
http://www.iteye.com/wiki/Rails-EveryDay/1150-%E6%AF%8F%E5%A4%A9%E4%B8%80%E5%89%82Rails%E8%89%AF%E8%8D%AF%E4%B9%8Bacts_as_ferret
(備注:a.插件安裝地址不在svn,而在github,也就是我上面提供的那個地址;
b.members = Member.find_id_by_contents("Gregg") =>這里面的"find_id_by_contents"方法已經棄用,應改為" find_id_with_ferret")
(2)進階篇:
http://www.iteye.com/topic/251038
(3)精華篇:
http://www.cnblogs.com/rubylouvre/archive/2009/07/22/1528544.html
(4)參考代碼
model:
acts_as_ferret
controller:
def search
@page_title = "Search"
if params[:commit] == "Search" || params[:q]
@books = Book.find_with_ferret(params[:q].to_s.upcase)
unless @books.size > 0
flash.now[:notice] = "No book found matching your criteria"
end
end
end
view:
search.rhtml
<%= render :partial => "search_box" %>
<% if @books %>
<p>Your search "<%= params[:q] %>" produced
<%= pluralize @books.size, "result" %>:</p>
<%= render(:partial => "books") %>
<% end %>
_search_box
<% form_tag({:action => "search"}, {:method => "get"}) do %>
<%= text_field_tag :q %>
<%= submit_tag "Search" %>
<% end %>