2013-03-06 36 views
-1

在products_controller.rb導軌 - find_by_ *航線

# GET /search/'brand' 
    def brand 
    @product = Product.find_all_by_brand(params[:brand]) 

    respond_to do |format| 
     format.html # brand.html.erb 
    end 
    end 

在routes.rb中

match '/search/:brand' => 'products#brand' 

,如果我嘗試訪問localhost:3000/search/Apple我收到以下錯誤Couldn't find Product with id=Apple

有什麼,我錯過了?是否還有其他文件需要處理?

更新

現在我越來越undefined method 'size' for nil:NilClass,我甚至不知道我改變。

由rails執行的查詢是select "products".* FROM "products" WHERE "products"."brand" = 'Apple' ORDER BY last_seen DESC,它們似乎返回正確的產品。

應用程序跟蹤

應用/視圖/產品/ _product.html.erb:1: _app_views_products__product_html_erb___2255278_29707176' app/views/products/brand.html.erb:1:in _app_views_products_brand_html_erb___464952485_38589588' 應用程序/控制器/ products_controller.rb:52:在`品牌」

+0

@ house9,對不起,但我不明白你在說什麼。 – rlhh 2013-03-06 01:07:51

+0

顯示視圖代碼'app/views/products/_product.html.erb'和調用代碼'app/views/products/brand.html.erb' - 您有一個無對象,也許您希望有一個產品目的? – house9 2013-03-06 16:14:08

回答

0

發現錯誤,erb頁面正在收到product而不是products。輸入錯誤。

1

控制器中的註釋表明您可能想要嘗試/搜索/品牌/ Apple。

+0

'rake routes'返回給我'/search/:brand(.:format)products#brand',是不是習慣於這樣寫呢? (我模仿默認的'show'給出的內容) – rlhh 2013-03-06 01:10:54

+0

您是否從另一個控制器複製並粘貼了控制器中的所有代碼?時髦的東西正在發生。你還做了一個耙分貝:遷移? – Stone 2013-03-06 01:13:46

+0

你是什麼意思的東西質樸?所有內容都是從書籍/網站上的示例中重新編寫而成 – rlhh 2013-03-06 01:15:19