Dobabeswe
Dobabeswe

Reputation: 103

Rails 3.2.1 Routing Error - No route matches

I'm getting the following error:

Routing Error

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

This is what I have in my routes.rb file:

resources :lists do 
  resources :tasks
end

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

root :to => 'lists#index'

In my 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

And in show.html.erb (where the error occurs):

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

Can someone please tell me what I'm doing wrong?

Upvotes: 0

Views: 406

Answers (1)

Erez Rabih
Erez Rabih

Reputation: 15788

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.

Upvotes: 1

Related Questions