0

In Rails 3.2.8 site, I get the following error:

Routing Error
No route matches {:controller=>"tasks", :action=>"complete", :list_id=>1, :id=>nil}

In my routes file I have the following:

resources :lists do
  resources :tasks
end

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

In the view:

<% @list.tasks.each do |task| %>
  <li><%= task.description %> - <%= button_to "Complete", 
                                    complete_task_path(@list.id, task.id) %></li>
<% end %>

When doing rake routes:

complete_task        /lists/:list_id/tasks/:id/complete(.:format) tasks#complete
Dobabeswe
  • 83
  • 1
  • 7

2 Answers2

0

Why not just change it to the following?

<%= button_to "Complete", "/lists/#{@list.id}/tasks/#{task.id}/complete" %>

Rails magic is meant to save time - hence it exists - but if it takes more time to figure out how to use it, then it may make sense to sometimes elect not to use that magic ;)

And just for debugging purposes, as this might solve your original problem, I would add the following just below your existing li tag, in order to observe the variables, and to make sure that none of them are nil:

<li>List ID: <%=@list.id%>, Task ID: <%=task.id%></li>
eriklinde
  • 431
  • 6
  • 7
0

This error is likely because one or more of the tasks associated with @list have a nil id. Recent versions of Rails are very picky about having non-nil ids to generate paths.

The error you are getting is trying to tell you that something is wrong (you're generating routes for objects that have not yet been persisted).

While @eriklinde's solution may "work" (probably outputting an invalid path with a missing :id value), it's not good form. Building the url manually makes it harder to update your paths later and likely is hiding a problem with your app. Wherever possible you should use the xxx_path helps as you did in your example.

tihm
  • 620
  • 4
  • 6