【问题标题】:In Rails 6, a link for delete without resource route is not working在 Rails 6 中,没有资源路由的删除链接不起作用
【发布时间】:2021-04-18 09:10:48
【问题描述】:

对于 Rails 6 中的 CRUD 操作,我没有使用资源路由。我写了一个 html 链接来删除带有<a></a> 标签的帖子。此链接不是删除,而是通过获取请求重定向到显示页面。

这是链接

<a href="/posts/<%= post.id %>" data-method="delete" >Delete</a>

这是 routes.rb 文件

Rails.application.routes.draw do

  get '/posts', to: 'posts#index'
  get '/posts/new', to: 'posts#new'
  post '/posts', to: 'posts#create'
  get '/posts/:id', to: 'posts#show'
  get '/posts/:id/edit', to: 'posts#edit'
  patch '/posts/:id', to: 'posts#update'
  delete '/posts/:id', to: 'posts#destroy'

end

这里是posts_controller.rb

def destroy
   @post.destroy
   respond_to do |format|
     format.html { redirect_to posts_url, notice: "Post was successfully destroyed." }
     format.json { head :no_content }
   end
 end

这是 application.js 文件

require("@rails/ujs").start()
require("turbolinks").start()
require("@rails/activestorage").start()
require("channels")

【问题讨论】:

  • 查看页面源码中是否有

标签: ruby-on-rails routes crud


【解决方案1】:

这里是创建指向删除方法的链接的 Rails 方法:

your_posts_path(id), method: :delete, data: { confirm: "You sure?" } %>

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多