0

我收到以下错误:

Routing Error

No route matches {:controller=>"tasks", :action=>"complete", :list_id=>1, :id=>nil}
Try running rake routes for more information on available routes.

这就是我的 routes.rb 文件中的内容:

resources :lists do 
  resources :tasks
end

match 'lists/:list_id/tasks/:id/complete' => 'tasks#complete', :as => :complete_task

root :to => 'lists#index'

在我的任务控制器中:

attr_accessor :completed
before_filter :find_list

def create
  @task = @list.tasks.new(params[:task])
  if @task.save
    flash[:notice] = "Task created"
redirect_to list_url(@list)
  else
flash[:error] = "Could not add task at this time."
redirect_to list_url(@list)
  end
end

def complete
  @task = @list.tasks.find(params[:id])
  @task.completed = true
  @task.save
  redirect_to list_url(@list)
end

private
  def find_list
    @list = List.find(params[:list_id])
  end

在 show.html.erb (发生错误的地方):

<%= button_to "Complete", complete_task_path(@list.id,task.id) %>

有人可以告诉我我做错了什么吗?

4

1 回答 1

1

What's causing the problem is that task.id in your show view returns nil, while in your routes:

match 'lists/:list_id/tasks/:id/complete' => 'tasks#complete', :as => :complete_task

Requires a task id in order to match the url pattern.

You can read more about it in this blog post.

于 2012-10-03T23:03:19.653 回答