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!

1 Answer 1

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 %>
Sign up to request clarification or add additional context in comments.

1 Comment

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: stackoverflow.com/questions/1159107/rails-nested-resource-issue

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.