简体   繁体   English

Ruby on Rails:删除重定向到显示页面的链接

[英]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.

相关问题 事件监听器(点击时,链接到显示页面)仅在 Ruby on Rails 项目中工作一次 - Event listener (on click, link to show page) only working once in Ruby on Rails project 为什么Delete链接在这个Ruby on Rails项目中不起作用? - Why Delete link doesn't work in this Ruby on Rails project? Ruby on Rails:为什么我的删除链接不起作用? - Ruby on Rails: Why doesn't my delete link work? 在轨道上使用Sweetalert删除红宝石的方法 - Delete method with sweetalert in ruby on rails 链接到关闭页面轨道4 - Link To Close Page Rails 4 在javascript onclick功能完成之前链接重定向页面 - Link redirects page before javascript onclick function is able to finish 使用锚标记打开模态时,页面重定向到空白链接 - Page redirects to a blank link when using anchor tag to open a modal 重定向到另一个页面的 Href 链接具有特定的操作 - Href link that redirects to another page has a specific action Stripe Checkout 订阅付款失败重定向到过期链接页面 - Stripe Checkout Subscription Payment failure redirects to Expire Link Page 根据单选按钮值在页面加载时显示/隐藏 div - Jquery 和 Ruby on Rails 简单表单 - Show / hide div on page load depending on radio button value - Jquery and Ruby on Rails Simple Form
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM