[英]Ruby on Rails: Delete link to redirects to show page
I'm working on a rails 5.0 application.我正在开发 Rails 5.0 应用程序。 It's a legacy application and has been poorly maintained over the years and god knows what hasn't been updated on it properly (for example, its still using a
public/
structure to contain all of its stylesheet and javascript files).这是一个遗留应用程序,多年来一直维护不善,天知道它有什么没有正确更新(例如,它仍然使用
public/
结构来包含它的所有样式表和 javascript 文件)。
I'm having trouble getting the default rails delete routes to execute.我在获取要执行的默认 rails delete 路由时遇到问题。
Consider the following controller action:考虑以下 controller 操作:
class Admin::PackagesController < ApplicationController
def destroy
@package = Package.find(params[:id])
@package.destroy
respond_to do |format|
format.html { redirect_to root_url}
format.xml { head :ok }
end
end
But when I call the following link:但是当我调用以下链接时:
<%= link_to 'Destroy', ['admin', package], :confirm => 'Are you sure?', :method => :delete %>
It doesn't call the destroy action: it goes to the show action for the individual package.它不会调用销毁操作:它会转到个人 package 的显示操作。
I'm not sure if this is still an issue but I ran into it with a Rails 7 blog/comments app.我不确定这是否仍然是个问题,但我在使用 Rails 7 博客/评论应用程序时遇到了这个问题。 The following code within a 'comment' partial (for Post show page) and a comment controller fix solved the issue for me.
“评论”部分(用于展示后页面)中的以下代码和评论 controller 修复为我解决了这个问题。
_comment.html.erb: _comment.html.erb:
<%= link_to 'Delete', [comment.post, comment], data: { turbo_method: :delete, turbo_confirm: 'are you sure?' }, class: 'button is-danger' %>
comments_controller.rb: comments_controller.rb:
def destroy
@post = Post.find(params[:post_id])
@comment = @post.comments.find(params[:id])
@comment.destroy
redirect_to post_path(@post), status: :see_other
end
The status: :see_other
section of the redirect was essential in it working and understanding how Rails handles redirects and this link helped a lot! status: :see_other
重定向的部分对于它的工作和理解 Rails 如何处理重定向至关重要,这个链接帮助很大!
<%= link_to 'Destroy', package_path(package), method: :delete %>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.