title: Using form_for with Nested Resources
body: <p>I am building a To Do application in an attempt to get fluent with Rails. There are four levels of hierarchy in my app. </p>
  
  <ol>
  <li>User</li>
  <li>Goals  (multiple goals per user)</li>
  <li>Tasks  (multiple tasks per goal)</li>
  <li>Subtasks (multiple subtasks per task)</li>
  </ol>
  
  <p>I have a working front end form for each of these that utilizes nested resources. My routes.rb has something like this </p>
  
  <pre><code> resources :goal do
    resources :task do
      resources :subtask
    end
   end
  </code></pre>
  
  <p>What I would like to do now is that have all these forms right in one of the views of the user controller. </p>
  
  <p>This the form that I have attempted to create :</p>
  
  <pre><code>&lt;%= form_for @task, url: {controller: 'task', action: 'create'} do |f| %&gt;
  
   &lt;%= f.label :description %&gt;
  
   &lt;%= f.text_field :description %&gt;
  
   &lt;%= f.submit "Add Goal", class: "btn" %&gt;
  
  &lt;% end %&gt;
  </code></pre>
  
  <p>But I end up getting this error </p>
  
  <pre><code>No route matches {:action=&gt;"create", :controller=&gt;"task", :id=&gt;"1"}
  </code></pre>
  
  <p>The :id=>1 corresponds to the user page I am on (<a href="http://localhost:3000/user/1" rel="nofollow">http://localhost:3000/user/1</a>)</p>
  
  <p>What I understand is that there is that nowhere have I provided the goal_id for which this step is intended. No idea how to implement this. </p>
  
  <p>Another thing that I have noticed is that a response to <code>rake routes</code> shows a lot of URI paths but nothing for POST method. It does not allow me to use a path from there in the <code>url:</code> in <code>form_for</code> because it does not match the POST method. </p>
  
  <p>So my questions are :</p>
  
  <ol>
  <li>How to route a form_for that when you have nested resources?</li>
  <li>How to provide the ID of the parent resource while using form_for so that my create action is correctly routed? </li>
  </ol>