I have a nested resources:
resources :bills do
resources :debts
end
and when I make a delete link in the index html in the debts view like this:
<td>
<%= link_to "Delete", [@bill, @debt], confirm: "Are you sure?", method: :delete %>
</td>
the bill is deleted, not the debt.
What happens?, How can I deleted only one debt of a specific Bill?
This is my delete action in my debt’s controller.
def destroy
@bill = Bill.find(params[:bill_id])
@debt = @bill.debts.find(params[:id])
@debt.destroy
flash[:notice] = "The debt was successfully deleted"
redirect_to bill_debts_url
end
And my models:
Bill model:
class Bill < ActiveRecord::Base
has_many :debts
end
Debt model:
class Debt < ActiveRecord::Base
belongs_to :bill
end
Thanks in advance!
You have a
has_manyassociation. If abillhas_manydebts, thenbill.debtsis an association, not a single object. You need to calldestroy_allon that object to destroy all of them:That being said, I’m not sure why the
Billis being destroyed at all…