2

In my Ruby on Rails code, I have the following edit.html.erb file for tasks:

<%= render 'form' %>

I then have a _form template in the same directory with the following code:

<%= form_for @task do |f| %>
    <%= fl.label :title %><br />
<% end %>

The problem is that I am getting an error when trying to navigate to the edit page. The error says "undefined task_path", so from what I can tell Rails is not properly identifying the path to my task.

The way the program is structured is that I have a List with many tasks, and each task has a list. The routes file declares the structure in this way:

resources :lists do
    resources :tasks
end

How do I get the form_for to identify that I am trying to edit a task at /lists/:list_id/tasks/:task_id/edit?

Thank you for your help!

Mike Caputo
  • 1,156
  • 17
  • 33

1 Answers1

6

You are using Nested Resources the proper way to use that in a form is specifying the parent.

<%= form_for [@list, @task] do |f| %>
  <%= f.label :title %><br />
<% end %>
JCorcuera
  • 6,794
  • 2
  • 35
  • 29
  • 1
    Just a note to anyone who finds this, like I did, looking for help with a nested resource _which has a named route_: Rails cannot deduce the URL for the form from an array, so you will need to specify the URL explicitly as something like `form_for (@task, url: my_custom_named_list_task_path(@list))`. As per: http://stackoverflow.com/questions/1159107/rails-nested-resource-issue – Leo Sep 08 '12 at 12:03