Rails - 嵌套对象删除
我想删除 user
拥有的嵌套对象 book
。在user#show
页面中显示与该用户
相关的所有书籍
。除了每本书之外,还有一个删除
链接。这是我的代码:
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 %>
我知道这是错误的,但是如何我可以删除想要的书吗?
I want to delete the nested object book
, that is owned by a user
. In the user#show
page appears all the books
related to that user
. Besides each book there is a link to delete
it. Here is my code:
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
And in the user#show
page:
<%= link_to "Delete", user_book_path(current_user, book), :method => :delete %>
I know this is wrong, but how can I do it in order to deleted the wanted book?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您删除时,您可能会忘记它是嵌套资源这一事实。你知道你说的是哪本书,所以直接删除就可以了。
路线:
预订控制器:
查看:
When you are deleting you can forget about the fact that it's a nested resource. You know which book you are talking about, so you can just delete it directly.
Routes:
Book controller:
View: