我一直在使用Rails 3,并尝试使用Rails4。我在这里遵循ToDo教程:http : //www.codelearn.org/ruby-on-rails-tutorial/introduction-views-layouts-helpers-assets-管道
我正在尝试使用Rails的“ link_to”帮助程序方法删除ToDo项目,并且一直在在线查找并且在过去90分钟内无法使其正常工作,因此我决定寻求帮助。
这是我收到的错误消息:
Routing Error
No route matches [DELETE] "/todos/delete_path"
这是我从航站楼出发的“耙道”:
Prefix Verb URI Pattern Controller#Action
todos_index GET /todos/index(.:format) todos#index
todos_delete DELETE /todos/delete(.:format) todos#delete
这是来自内部的“ link_to”辅助方法:
index.html.erb
<%= link_to 'Delete last todo', 'delete_path', method: :delete %>
这是我的:
routes.rb
Rails.application.routes.draw do
get 'todos/index'
# get 'todos/delete'
match 'todos/delete' => 'todos#delete', via: :delete
这是我的:
todos_controller.rb
class TodosController < ApplicationController
def index
# @todo_array = ['Buy Milk', 'Buy Soap', 'Pay bill', 'Draw Money']
@todo_items = Todo.all
render :index
end
def delete
#put delete logic here
@todo_items = Todo.last
@todo_items.destroy
end
end
感谢您抽出宝贵的时间来研究我的问题!
这是您编写destroy方法的方式:
def destroy
@todo = Todo.last
@todo.destroy
redirect_to todos_index_path #or wherever you want to redirect user after deleting the tod
end
另外,我只是使用RESTful路由:
Rails.application.routes.draw do
resources :todos, only: %i(index destroy)
end
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句