Multiple has_many objects in one form

Hi - I have to models, Question and Answers. A question has many
answers. I want to set up a form where a user can submit 1 question
and up to 10 answers. So, in the questions controller, I have new:

def new
@question = Question.new
@answers=[]
i=0
while i<10
@answers << Answer.new
i += 1
end
end

In the form, I’m trying something like:

<% form_for :question do |f|%>
Question: <%=f.text_field :question%>

<%for @answer in @answers %>
<%fields_for :answer do|a|%>
Answer: <%=a.text_field :answer%>

<%end%>
<%end%>

<%end%>

The problem is, this doesn’t index the answers, the html just shows 10
repeated answer[answer]. Is there a better way?

The second question I have is, how do I validate for at least 1
answer?

Thanks,
Dino

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs