2010-11-18 34 views
6

我無法弄清楚如何在rails 3.0中做到這一點。我有一個控制器,products和動作,search,並在routes.rb我已經試過配置不安寧的路由

resources :products, :collection => {:search => :post } 

match 'products/search' => 'products#search', :via => [:get, :post] 

和許多其他設置,但每當我訪問products/search我還得到一個錯誤抱怨找不到行動show的ID爲search的產品。任何人都知道我在做什麼錯了?

謝謝。

回答

10

你很近。

resources :products do 
    collection do 
    match 'search', :via => [:get, :post] 
    end 
end 

或者,你也可以這樣做:

resources :products do 
    match 'search', :on => :collection, :via => [:get, :post] 
end 

看到邊緣指南的Rails Routing from the Outside In更多的信息,更具體:

+1

哇,我不知道我是如何錯過「從外部路由」頁面的那一部分。真棒。謝謝,夥計們,工作。 – user508546 2010-11-18 17:19:23

+0

沒有問題。很高興我們可以提供幫助。 =)這太糟糕了,你不能把我們兩個都標記爲答案。 = d – John 2010-11-18 17:21:13

4

在Rails 3,collection現在是一個塊:

resources :products do 
    collection do 
    get :search 
    post :search 
    end 
end 

這將可以訪問使用一個GETPOST請求ProductsController#search動作。