0

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...

1 Answer 1

2

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

quote_charge_path([@quote, charge])
Sign up to request clarification or add additional context in comments.

1 Comment

Hey @carlosramireziii, for some reason the url that's being generated from this is <a href="/quotes/13/11/charges/13" .... Any idea why there would be that initial 13 after quotes?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.