本文介绍了Rails删除链接嵌套路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个简单的待办应用程序,其中包含项目,并且项目有很多任务。我希望能够删除项目中的任务。但是当我尝试链接到删除方法时,会得到未定义的方法'task_path'。

I'm writing a simple todo app which has projects and projects has many tasks. I want to be able to delete a task within a project. But when I try to link_to a delete method I get undefined method 'task_path'.

查看代码

<ul>
  <% @project.tasks.each do |task| %>
    <li><%= task.name %></li> <%= link_to "delete", task, :method  => :delete %></br>
  <% end %>
</ul>

任务控制器

  def destroy
    @project = Project.find(params[:project_id])
    @task = @project.tasks.find(params[:id])
    @task.destroy
    redirect_to @project, :notice => "Task Deleted"
  end

routes.rb

routes.rb

 resources :projects do
    resources :tasks
  end

更新:
所以我删除了。但是现在当我遍历每个任务时,还有一个额外的删除链接,该链接链接到并没有路由匹配[DELETE] / projects / 9 / tasks为什么在其中有多余的删除链接?

Update:So I have delete working. But now as I'm iterating through each task, there's an extra delete link which routes to http://todoapp.dev/projects/9/tasks and gives No route matches [DELETE] "/projects/9/tasks" Why is the extra delete link in there?

 <% @project.tasks.each do |task| %>
    <%= task.name %> <%= link_to "delete", [@project, task], :method  => :delete %>
  <% end %>

推荐答案

请尝试以下操作:

<%= link_to "delete", [@project, task], :method  => :delete %>

让我知道

这篇关于Rails删除链接嵌套路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 01:38