如何在Rails網站里加搜索功能(acts_as_ferret備忘錄)

如何在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 %>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值