我正在努力添加一个链接来删除嵌套的对象。我有一个列表应用程序,其中嵌套了许多这样的任务。
resources :lists do
resource :tasks
end
我读过这篇文章,但它不适合我。我想在
下面的代码中删除一个链接 <h4>Tasks</h4>
<% @list.tasks.each do |task| %>
<%= task.desc %>
<%= link_to "Delete", list_task_path(list, task), :method => :delete %><br />
<% end %>
但是它给了我以下错误
undefined local variable or method `list' for #<#<Class:0x007f9adc555db0>:0x007f9adc547828>
编辑:现在我得到以下错误当我刷新页面
No route matches {:action=>"show", :controller=>"tasks", :list_id=>#<List id: 28, name: "Julklappar", user_id: 1, created_at: "2012-12-22 17:40:05", updated_at: "2012-12-22 17:40:05">, :id=>#<Task id: nil, desc: nil, completed: false, list_id: 28, created_at: nil, updated_at: nil>}
Rake routes:
pierre@ubuntu:~/todolist$ rake routes
list_tasks GET /lists/:list_id/tasks(.:format) tasks#index
POST /lists/:list_id/tasks(.:format) tasks#create
new_list_task GET /lists/:list_id/tasks/new(.:format) tasks#new
edit_list_task GET /lists/:list_id/tasks/:id/edit(.:format) tasks#edit
list_task GET /lists/:list_id/tasks/:id(.:format) tasks#show
PUT /lists/:list_id/tasks/:id(.:format) tasks#update
DELETE /lists/:list_id/tasks/:id(.:format) tasks#destroy
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
PUT /lists/:id(.:format) lists#update
DELETE /lists/:id(.:format) lists#destroy
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/register(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
root / lists#index
编辑# 2这是我的lists/show。html。erb文件
<p id="notice"><%= notice %></p>
<h2><%= @list.name %></h2>
<h4>Tasks</h4>
<% @list.tasks.each do |task| %>
<%= task.desc %>
<%= link_to "Delete", list_task_path(@list, task), :method => :delete %>
<% end %>
<%= form_for [@list, @task] do |form| %>
<p><%= form.text_field :desc %> <%= form.submit %></p>
<% end %>
<%= link_to 'Edit', edit_list_path(@list) %> |
<%= link_to 'Back', lists_path %>
列表控制器
def show
@list = current_user.lists.find(params[:id])
@task = @list.tasks.new
end
你的链接应该是:
<%= link_to "Delete", list_task_path(@list, task), :method => :delete %>
当循环通过@list.tasks
时,您只通过|task|
定位task
-但@list
始终保持@list
。
另一个问题是在你的控制器:@task = @list.tasks.new
-你在这里建立一个新的任务,这是你的表单所需要的。然而,这个任务是新的,这意味着它还没有被保存——您根本不能建立一个链接来删除它,因为它还不存在于数据库中。您可以通过直接在表单中而不是在控制器中构建任务来避免这种情况。
删除控制器中的这一行:
@task = @list.tasks.new
并将表单更改为:
<%= form_for [@list, @list.tasks.build] do |form| %>