我想刪除嵌套對象book
,它由user
擁有。在user#show
頁面出現所有與user
相關的books
。除了每本書,還有一個鏈接到delete
它。這裏是我的代碼:導軌 - 嵌套對象刪除
routes.rb
:
resources :users do
resources :books, :only => [:new, :create, :destroy]
end
book_controller.rb
:
def destroy
@user= User.find(params[:user])
@book = Book.find(params[:book])
@book.destroy
redirect_to current_user
end
而在user#show
頁:
<%= link_to "Delete", user_book_path(current_user, book), :method => :delete %>
我知道這是錯的,但我怎麼能這樣做是爲了刪除通緝書嗎?
你向我們展示的代碼並沒有太大的意義。在哪個控制器中是'destroy'方法?書和用戶有什麼關係? – Mischa 2011-05-30 15:05:44
要編輯問題。 – Samth 2011-05-30 15:08:22
謝謝。我現在明白你的問題。 – Mischa 2011-05-30 15:16:43