Search code examples
ruby-on-railsrubyform-fornested-resources

Ruby on Rails form_for causing path error


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!


Solution

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