I am trying to add an custom action to a resource but I get a routing error No route matches [GET] "/products/list/up". I have tried commenting out the URI in routes.rb, but they also don’t work. What am I doing wrong?
I have this in the 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
in the products_controller.rb:
def product_list
@products= Product.all
respond_to do |format|
format.json { render json: @products.to_json}
end
end
Try
collectioninstead ofmemberas you don’t provide anyidin the path:Running
rake routesgives: