2012-07-13 132 views
2

我是RoR中的新成員。根據這本書(ruby on rails 4th edition),我已經完成了它。但我正在嘗試爲客戶提供搜索選項,以便更輕鬆地找到該產品。我用這個例子http://railscasts.com/episodes/37-simple-search-form搜索數據庫ror

我已經把意見>商店>指數這一努力:

<%= form_tag products_path, :method => 'get' do %> 
    <p> 
    <%= text_field_tag :search, params[:search] %> 
    <%= submit_tag "Search", :name => nil %> 
    </p> 
<% end %> 
在模型

>產品我已經把這個:

def self.search(search) 
    if search 
    find(:all, :conditions => ['title LIKE ?', "%#{search}%"]) 
    else 
    find(:all) 
    end 
end 

,並在控制器> store_controller這個:

@products = Product.search(params[:search]) 

問題是當我搜索是顯示我所有th e產品我有,不僅我的搜索和網址正在改變爲..... /?utf8 =%E2%9C%93 &搜索= iphone + 4

任何想法plz?

回答

2

好,我找到了!最後...

在store_controller我在def索引這行:@products = Product.all並導致問題。沒有store_controller是:

class StoreController < ApplicationController 
    skip_before_filter :authorize 
     @products = Product.all 
    def index 
    @products = Product.search(params[:search]) 

    @cart = current_cart 
    end 
end 

和工程就像瘋了一樣!

4
<% form_tag ... 

必須是:

<%= form_tag ... 
^
+0

沒有任何區別 – marios 2012-07-15 22:02:37