3

I'm having issues with my destroy method on a nested source Product, that is tied to Orders.

After attempting to destroy an item, I'm redirecting users to my order_products_url. I receive the following routing error:

No route matches "/orders/1/products"

My destroy method looks like this:

def destroy
    @product = Product.find(params[:id])
    @order = Order.find(params[:order_id])
    @product.destroy

    respond_to do |format|
      format.html { redirect_to(order_products_url) }
      format.xml  { head :ok }
    end
end

And in routes.rb:

resources :orders do
    resources :products, :controller => "products"    
  end

The reason why this is confusing me, is for my update method for products, I'm properly redirecting users to the order_products_url without issue. I don't understand why it works there but not here.

Thanks

Kombo
  • 2,371
  • 3
  • 34
  • 64

3 Answers3

8

order_products_url expects a parameter to be passed - either the order id, or the order object itself. Without this, it won't work properly. So using your code above:

def destroy
    @product = Product.find(params[:id])
    @order = Order.find(params[:order_id])
    @product.destroy

    respond_to do |format|
      format.html { redirect_to(order_products_url(@order) }
      format.xml  { head :ok }
    end
end

As a side note, you can shorten your routes a little:

resources :orders do
  resources :products
end

Specifying the controller is redundant when it's named as Rails expects. I hope this helps!

UPDATE: I've added a link to my article about routing in Rails 3, with downloadable code samples. I updated it with a paragraph that explains named routes, in the "Things You Should Know" section:

Routing in Ruby on Rails 3

Jaime Bellmyer
  • 23,051
  • 7
  • 53
  • 50
  • Sweet,thanks for the help. After correcting this, I found an issue with my view, where I've now corrected it to where using a delete method on my order_product_path rather than before where I was trying to delete just my @product. I appreciate the help, I really do. – Kombo Dec 09 '10 at 20:45
  • I'm glad it helped! If you want more info, I added a link to a blog post I did a while back. Good luck! – Jaime Bellmyer Dec 09 '10 at 21:00
  • Also helpful to note is that you include @order in the redirect route -- order_products_url(@order) -- because you need to tell Rails which specific product index you want to show (i.e. the index of products nested under that specific order, not some other order). – Ethan Jun 10 '11 at 02:06
3

Don't you need to redirect to order_products_url(@order)?

Cory
  • 2,538
  • 2
  • 18
  • 19
1

you should be using orer_products_path (not url). If you go to the root of your app and type,

rake routes

that will give you a list of all named routes. You need to append _path to them though (returns the string representation). This is a handy little trick for figuring out named routes.

Now to your real question - of course it doesn't exist! You just deleted it! You are destroying the product instead of the product from the order!

sethvargo
  • 26,739
  • 10
  • 86
  • 156
  • Both suffixes (_path and _url) work in this instance, it's the missing parameter that is causing the issue. – Jaime Bellmyer Dec 09 '10 at 19:56
  • Hey Seth, thanks for to help. rake routes was very helping my solve another problem I created once I found my issue from not passing an arguement(doh). For a test, I tried using both _path and _url - both worked. I'm just curious what's the difference between the two? – Kombo Dec 09 '10 at 20:46
  • _path is relative, _url is absolute http://stackoverflow.com/questions/2350539/what-is-the-difference-between-url-and-path-while-using-the-routes-in-rails – Art Shayderov Dec 09 '10 at 21:11