Rails 3.2.1路由错误 - 没有路由匹配

时间:2012-10-03 18:12:30

标签: ruby-on-rails ruby-on-rails-3 routes

我收到以下错误:

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'

在我的tasks_controller中:

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) %>

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

1 个答案:

答案 0 :(得分:1)

造成问题的原因是您的展示视图中的task.id在您的路线中返回nil:

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

需要任务ID才能匹配网址格式。

您可以在this blog post中了解更多相关信息。