完全难倒这一点,因为我相信它使用了正确的 Restful 设置,但我似乎无法删除我的列表。
我有以下几点:
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
索引.html.haml
- @list.each do |list|
...
= link_to "Delete", list_path(list), method: :delete, data: { confirm: 'Are you certain you want to delete this?' }
...
路线:
ListApp::Application.routes.draw do
devise_for :users
resources :lists
这给出了正确的 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
但是我在生产环境中运行时仍然收到以下错误。
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'
如果有人知道可能导致这种情况的原因,我们将不胜感激:)
最佳答案
更新了答案。
您的 link_to
声明看起来不错:
= link_to "Delete", list_path(list), method: :delete, data: { confirm: 'Are you certain you want to delete this?' }
您的布局文件中可能没有包含必要的 javascript:
# app/views/layouts/application.html.haml
%head
= javascript_include_tag "application"
= csrf_meta_tag %>
这就是为什么
list_path
无法执行 destroy
操作并试图执行 show
行动。如果这不起作用,那么问题出在其他地方,阻止了 javascript 的执行。
关于ruby-on-rails - 销毁路径在 Ruby on Rails 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18723155/