0

导轨 3.2。在这里,我想指示 allhttp://domain.dev/toys只显示所有shopsshop_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

我做错了什么?谢谢。

4

2 回答 2

2

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

约束用于路段。

(好吧,还有动词,但它们是用:via; 或方法指定的Request。)

于 2012-05-08T17:06:10.350 回答
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
于 2012-05-08T18:26:01.290 回答