Tony Beninate
Tony Beninate

Reputation: 1985

Rails Delete a record from nested-resource

I have a Charge model that is a subset of a Quotes model.

resources :quotes do
  resources :charges
end

I want to delete a charge while on an edit page of a quote.

quotes/_form.html.haml

- @quote.charges.each do |charge|
  %tr
    %td
      = link_to 'x', charge, :confirm => 'Are you sure you want to delete this charge?', :method => :delete

When I load the page I get undefined method `charge_path' for #<#:0x0000012f8a2ea8>. I've also tried:

= link_to 'x', quote_charge_path(charge), :confirm => 'Are you sure you want to delete this charge?', :method => :delete

And that appears to work, but it seems to have the quote id and the charge id in the wrong order. For example, the URL that generates is <a href="/quotes/11/charges/13" ... It should actually be /quotes/13/charges/11. Anyone know how to do this correctly? Thanks.

Update

Interestingly, what ended up working was = link_to '×', [@quote, charge], :confirm => 'Are you sure you want to delete this charge?', :method => :delete, :class => "close". When I was specifying the path quote_charge_path([@quote, charge]) it was creating the wrong url. Not sure why...

Upvotes: 0

Views: 561

Answers (1)

Carlos Ramirez III
Carlos Ramirez III

Reputation: 7434

For nested route helpers you need to pass in both the parent and the child object. Try

quote_charge_path([@quote, charge])

Upvotes: 2

Related Questions