0

我正在尝试向资源添加自定义操作,但出现路由错误No route matches [GET] "/products/list/up"。我尝试在 routes.rb 中注释掉 URI,但它们也不起作用。我究竟做错了什么?

我有这个routes.rb

 namespace :api, :defaults =>{format: 'json'} do
 scope module:  :v1 ,constraints: ApiConstraints.new(version:1, default: true) do

resources :products do
member do
  match  "/list/up" =>"products#product_list" ,:via=>:get
  #get "/list/up" , :action=>"product_list"
  #get "/list/up" , :to=>"product_list"
end
end
 end
 end

products_controller.rb

def product_list
  @products= Product.all
  respond_to do |format|
    format.json { render json: @products.to_json}
  end
end
4

1 回答 1

1

尝试collection代替,member因为您没有id在路径中提供任何内容:

resources :products do
    collection do # <-----<
        get  "/list/up" =>"products#product_list"
    end
end

运行rake routes给出:

list_up_products GET    /products/list/up(.:format)  products#product_list
        products GET    /products(.:format)          products#index
                 POST   /products(.:format)          products#create
     new_product GET    /products/new(.:format)      products#new
    edit_product GET    /products/:id/edit(.:format) products#edit
         product GET    /products/:id(.:format)      products#show
                 PUT    /products/:id(.:format)      products#update
                 DELETE /products/:id(.:format)      products#destroy
于 2012-11-19T18:14:43.963 回答