我一直在使用Rails 3并尝试使用Rails 4.我在这里关注ToDo教程:http://www.codelearn.org/ruby-on-rails-tutorial/introduction-views-layouts-helpers-assets-pipeline
我正在尝试使用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
感谢您抽出宝贵时间来查看我的问题!
答案 0 :(得分:2)
这就是编写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