views:

29

answers:

1

My question template lists answers, and lets somone add a new answer for a question.

But I'm not sure where and how to initialize a new answer for this line:

<%= link_to 'New answer', new_question_answer_path(@question, Answer.new) %>

from the context below:

<p>
  <b>Body:</b>
  <%=h @question.body %>
</p>

<h1>Listing answers</h1>

<table>
  <tr>
    <th>Body</th>
  </tr>

<% @question.answers.each do |answer| %>
  <tr>
    <td><%=h answer.body %></td>
    <td><%= link_to 'Show', answer %></td>
    <td><%= link_to 'Edit', edit_answer_path(answer) %></td>
    <td><%= link_to 'Destroy', answer, :confirm => 'Are you sure?', :method => :delete %></td>
  </tr>
<% end %>
</table>

<br />

<%= link_to 'New answer', new_question_answer_path(@question, Answer.new) %>



<%= link_to 'Edit', edit_question_path(@question) %> |
<%= link_to 'Back', questions_path %>

If I initialize the new Answer like this:

<%= link_to 'New answer', new_question_answer_path(@question, Answer.new) %>

the URL of the next page is garbled with an inspect call to the new Answer.

+2  A: 

Try this:

<%= link_to 'New answer', new_question_answer_path(@question) %>
khelll
Correct. @answer= Answer.new belongs in the controller under the new action.
hgimenez