![](/img/trans.png)
[英]Rails Nested Route: Delete action doesn't work for nested resource when displaying parent resource
[英]Delete route in a nested resource in rails 3.1
我有一個嵌套資源:
resources :bills do
resources :debts
end
當我在債務視圖的索引html中進行刪除鏈接時,如下所示:
<td>
<%= link_to "Delete", [@bill, @debt], confirm: "Are you sure?", method: :delete %>
</td>
賬單被刪除,而不是債務。 會發生什么?,如何刪除特定賬單中的一個債務? 這是我在債務控制者中的刪除操作。
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
我的模型:
帳單模型:
class Bill < ActiveRecord::Base
has_many :debts
end
債務模型:
class Debt < ActiveRecord::Base
belongs_to :bill
end
提前致謝!
您有一個has_many
關聯。 如果bill
has_many
debts
,則bill.debts
是一個關聯,而不是單個對象。 您需要在該對象上調用destroy_all
來銷毀所有對象:
def destroy
@bill = Bill.find(params[:bill_id])
@debts = @bill.debts.find(params[:id])
@debts.destroy_all
flash[:notice] = "The debt was successfully deleted"
redirect_to bill_debts_url
end
話雖這么說,我不確定為什么Bill
會被全部銷毀...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.