2012-07-13 66 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