Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails 4-如何路由删除路径_Ruby On Rails_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails Rails 4-如何路由删除路径

Ruby on rails Rails 4-如何路由删除路径,ruby-on-rails,ruby-on-rails-4,Ruby On Rails,Ruby On Rails 4,我一直在使用Rails 3,现在正在尝试使用Rails 4。下面是我的ToDo教程: 我正在尝试使用Rails的“link_to”助手方法删除ToDo项,并且一直在网上查找,在过去90分钟内无法使其正常工作,因此我决定寻求帮助 以下是我收到的错误消息: Routing Error No route matches [DELETE] "/todos/delete_path" 这是我从终点站出发的“rake路线”: Prefix Verb URI Pattern Con

我一直在使用Rails 3,现在正在尝试使用Rails 4。下面是我的ToDo教程:

我正在尝试使用Rails的“link_to”助手方法删除ToDo项,并且一直在网上查找,在过去90分钟内无法使其正常工作,因此我决定寻求帮助

以下是我收到的错误消息:

Routing Error
No route matches [DELETE] "/todos/delete_path"
这是我从终点站出发的“rake路线”:

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
todos_controller.rb
类TodosController

谢谢你抽出时间来看看我的问题

以下是编写销毁方法的方法:

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