2012-05-08 63 views
0

Rails 3.2。在这里,我想指示所有http://domain.dev/toys只显示所有shops其中shop_type(在我的表格栏中)是toys通过路径参数

# routes.rb 
resources :shops 

match 'toys' => 'shops#index', :as => :toys, :via => :get, :constraints => {:shop_type => 'toys'} 

# shops_controller.rb 
def index 
    @shops = Shop.find(:all) 
end 

我做错了什么?谢谢。

回答

2

错误部分:Shop.find(:all)

Constraints are for route segments.

(嗯,和动词,但随后他们与:via规定;或方法上Request

+0

心灵t o分享一些想法如何改善它? – Victor

+0

@Victor您需要使用适当的商店类型进行查找 - 不知道最干净的方法是什么,但最简单的方法是创建一个方法,然后调用它并呈现相应的模板。 –

0

routes.rb

match 'shops(/:shop_type)' => 'shops#index', :via => :get, :as => :shops_path 

shops_controller.rb

SHOP_TYPES = [:toys, :clothing, :accessories] 

def index 
    @shops = [] 
    if SHOP_TYPES.include? params[:shop_type].to_sym 
     @shops = Shop.find_all_by_shop_type(params[:shop_type]) 
    else 
     @shops = Shop.find(:all) 
    end 
end