Rails 3 form_for Nested Routes

I am trying to make form_for with nested routes, following the example of the blog and comments from the Ruby Guide (http://guides.rubyonrails.org/getting_started.html#adding-a-route-for-comments).

I am making an application for creating surveys with a large number of questions, the questions are in a group, and each question has another answer option.

This is reoutes.rb

  resources :groups do
    resources :questions do
      resource :answers
    end
  end

The controllers work well, and when I show the created group, you can see them and ask questions using the nested form: below:

<strong> groups /show.html.erb

<h2>Group: <%= @group.desc %> </h2>
<h3>Questions</h3>
<% @group.questions.each do |q| %>
   <%= q.desc%> <%= link_to 'Destroy question', [@group, q], :confirm => 'Are you sure?', :method => :delete %> <br/>
<%end%>

<h4>New question</h4>
<%= form_for([@group, @group.questions.build]) do |f| %>
  <div class="field">
    <%= f.label 'Label: '%>
    <%= f.text_field :desc, :size => 100%>
    <%= f.submit 'Create question'  %>
  </div>
<% end %>
<br />

Now I need to show the answers and somehow insert the underders to this question. To show the answers, it works well with q.answers.each inside @ group.questions.each. But I have to do form_for to answer, I tried the code below, but does`nt work:

/show.html.erb

...
<% @group.questions.each do |q| %>
   <%= q.desc%> <%= link_to 'Destroy question', [@group, q], :confirm => 'Are you sure?', :method => :delete %> <br/>
  <!-- New answer -->
  <%= form_for([q, q.answers.build]) do |f| %>
  <div class="field">
    <%= f.label 'Label: '%>
    <%= f.text_field :desc, :size => 100%>
    <%= f.submit 'Create answer'  %>
  </div>
<% end %> 

<%end%>
<h4> New question<h4>
...

Rails :

undefined `Question_answers_path '

form_for ([q, q.answers.build]).

?

+3
1

, , , , . - form_for [g,q,q.answers.build]. dosnt , , rake routes, .

+1

All Articles