Reputation: 447
i'm wondering if anyone can give me any advice.
I'm currently writing a rails API and although it doesn't seem like a best practice, rather than performing a DELETE call to
localhost:3000/products/:id
id rather make it to
localhost:3000/products/:url
and pass in the URL to be deleted, however i've currently got this but I keep getting a routing error.
DELETE '/products/:url', to: 'products#destroy'
is my current route for this, it is also specified above my
resources :products
sections.
My whole routes file:
AppName::Application.routes.draw do
resources :features do
resources :feature_links
end
resources :wishlist_items
resources :data_feeds
get '/get_data_feeds', to: 'data_feeds#get_feed_url', as: 'feed_url'
resources :genders
resources :trends
resources :sub_categories
resources :color_tags
resources :colors
resources :categories
delete '/products/:url', to: 'products#destroy'
resources :products do
member do
get 'buy'
post 'wish'
end
end
end
Any ideas? Thanks in advance
If the url i'm sending the delete request to is http://localhost:3000/products/www.test.com
I get the error No route matches [DELETE] "/products/www.test.com"
if the url I sent the delete request to is http://localhost:3000/products/:url
I get the error Couldn't find Product with 'id'=:url
My Destroy method code:
def destroy
@product = Product.find(params[:url])
@product.destroy
respond_with(@product, status: 200)
end
Upvotes: 0
Views: 2186
Reputation: 14402
I think Rails is considering your URL parameter as the specification of the format of the response. You can override the constraints of the parameter as follows:
constraints: { url: /[^\/]+/ }
This will make sure that the URL parameter can be anything except for /
. The whole route should look like this:
delete "/products/:url", to: "products#destroy", constraints: { url: /[^\/]+/ }, as: :products_destroy_with_url
And use it like this:
link_to "Destroy", products_destroy_with_url_path("www.test.com"), method: :delete
Upvotes: 2