销毁路径在 Ruby on Rails 中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18723155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Destroy path not working in Ruby on Rails
提问by Tom Pinchen
Totally stumped on this as I believe it's using the correct restful setup but I can't seem to delete my list.
完全难倒这一点,因为我相信它使用了正确的宁静设置,但我似乎无法删除我的列表。
I have the following:
我有以下几点:
Controller:
控制器:
def destroy
@list = List.find(params[:id])
@list.destroy
redirect_to lists_path
end
#37 def show
#38 @list = List.find(params[:id])
#39 end
Index.html.haml
索引.html.haml
- @list.each do |list|
...
= link_to "Delete", list_path(list), method: :delete, data: { confirm: 'Are you certain you want to delete this?' }
...
Routes:
路线:
ListApp::Application.routes.draw do
devise_for :users
resources :lists
Which is giving the correct rake routes output of:
这给出了正确的 rake 路由输出:
lists GET /lists(.:format) lists#index
POST /lists(.:format) lists#create
new_list GET /lists/new(.:format) lists#new
edit_list GET /lists/:id/edit(.:format) lists#edit
list GET /lists/:id(.:format) lists#show
PATCH /lists/:id(.:format) lists#update
PUT /lists/:id(.:format) lists#update
DELETE /lists/:id(.:format) lists#destroy
But I am still getting the following error when running in production environment.
但是我在生产环境中运行时仍然收到以下错误。
I, [2013-09-10T15:27:40.338022 #453] INFO -- : Started GET "/lists/2" for 81.159.35.212 at 2013-09-10 15:27:40 +0000
I, [2013-09-10T15:27:40.340626 #453] INFO -- : Processing by ListsController#show as HTML
I, [2013-09-10T15:27:40.340867 #453] INFO -- : Parameters: {"id"=>"2"}
I, [2013-09-10T15:27:40.354092 #453] INFO -- : Completed 500 Internal Server Error in 13ms
F, [2013-09-10T15:27:40.359807 #453] FATAL -- :
NameError (undefined local variable or method `list' for #<ListsController:0x000000050a7c38>):
app/controllers/lists_controller.rb:38:in `show'
If anyone knows what could be causing this any help would be much appreciated :)
如果有人知道可能导致这种情况的原因,我们将不胜感激:)
回答by vee
Updated answer.
更新了答案。
Your link_todeclaration looks fine:
您的link_to声明看起来不错:
= link_to "Delete", list_path(list), method: :delete, data: { confirm: 'Are you certain you want to delete this?' }
It' likely that you do not have necessary javascript included in your layout file:
您的布局文件中可能没有包含必要的 javascript:
# app/views/layouts/application.html.haml
%head
= javascript_include_tag "application"
= csrf_meta_tag %>
which is why the list_pathis not able to execute the destroyaction and is trying to execute the showaction.
这就是为什么list_path无法执行该destroy操作并正在尝试执行该show操作的原因。
If this does not work, then the problem is elsewhere that's preventing the javascript from being executed.
如果这不起作用,那么问题出在其他地方,阻止了 javascript 的执行。
回答by Carlos Drew
Looking at your log output, the link is sending a GET request instead of a DELETE request. In rails, a link with method: :deletegets changed from a GET request to a DELETE request via JavaScript.
查看您的日志输出,该链接正在发送 GET 请求而不是 DELETE 请求。在 Rails 中,method: :delete通过 JavaScript将链接从 GET 请求更改为 DELETE 请求。
My hunch is that you are not including the Rails default jquery javascript files either in your application.js or via = javascript_include_tag :defaults, which will get you jquery and jquery.ujs for Rails.
我的预感是您没有在 application.js 或 via 中包含 Rails 默认的 jquery javascript 文件= javascript_include_tag :defaults,这将为 Rails 提供 jquery 和 jquery.ujs。
Can you verify that jquery and jquery.ujs are loaded on your page with the delete link?
您能否通过删除链接验证 jquery 和 jquery.ujs 是否已加载到您的页面上?
Alternately, try adding = javascript_include_tag :defaultsto your view/layout and see if the link starts working.
或者,尝试添加= javascript_include_tag :defaults到您的视图/布局并查看链接是否开始工作。
Also, I suggest that you watch and follow this Railscasts on Unobtrusive Javascriptfor more information.
另外,我建议您在 Unobtrusive Javascript 上观看并关注这个 Railscasts 以获取更多信息。
回答by Rails Bluebelt
In your controller change the @list.destroy in the 'destroy method' to @list.delete . It worked for me in the past. Hope it helps.
在您的控制器中,将“销毁方法”中的 @list.destroy 更改为 @list.delete 。过去它对我有用。希望能帮助到你。
回答by NM Pennypacker
Try linking to lists_path instead of list_path. Look at your routes.
尝试链接到list_path 而不是list_path。看看你的路线。

