删除/销毁在 Rails 中不起作用 [英] Delete/Destroy is not working within Rails
本文介绍了删除/销毁在 Rails 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用我在 tags_controller 页面中编写的一段代码使删除按钮工作;
I am trying to get a delete button to work using a piece of code I have written within my tags_controller page;
def destroy
@tag = Tag.find(params[:id])
@tag.destroy
redirect_to :back, notice: 'Tag was successfully deleted!'
end
当我从本地主机运行时,它抛出如下所示的异常;
When I run from my local host, it throws an exception as shown below;
Routing Error
No route matches [DELETE] "/admin/tags/37/edit"
Rails.root: /Users/laurenwoodhams/Desktop/PROJECT/RAILS-BLOG/-t
这是我的配置路由;
Rails.application.routes.draw do
get '/login' => 'admin/sessions#new'
get '/logout' => 'admin/sessions#destroy'
namespace :admin do
resources :posts
resources :tags, except: [:index]
resources :sessions, only: [:new, :create, :destroy]
resources :administrators, only: [:index, :edit, :update]
end
end
能否请您检查并解释为什么会发生这种情况?
Could you please examine and explain why this may be occurring?
谢谢
推荐答案
DELETE 是 HTTP 动词(如 GET 或 POST),意思是..
DELETE is the HTTP verb (like GET or POST) which means..
要创建删除标签的链接,您需要在视图中执行类似操作.
To make a link to delete your tag you'll need to do something like this in your view.
<%= link_to "Delete Tag", @tag, method: "delete" %>
然后它应该可以正常工作.
Then it should work properly.
附言你可以运行 bin/rake routes
来查看你的路由.
P.S.
You can run bin/rake routes
to see your routes.
这篇关于删除/销毁在 Rails 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文