在路径中传递参数

时间:2012-05-08 16:55:46

标签: ruby-on-rails ruby-on-rails-3 routes

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

{{1}}

我做错了什么?感谢。

2 个答案:

答案 0 :(得分:2)

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

Constraints are for route segments.

(嗯,动词,但是它们是用:via指定的;或Request上的方法。)

答案 1 :(得分: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