I have three resources: Jobs, Questions and Answers.
The relationships are: Job has many questions; Question has many Answers.
I have created a nested form on the Jobs form view, which includes the creation of jobs and questions. Those are both going to be behind an admin wall, but I want the users to answer the questions through a form on the answers form view (not behind a wall).
The problem I am facing is that I want to create a loop for the answers form fields.
Since this is a loop and there will be more than 1 answer field, I want the questions to dynamically render as the answer form labels. This would be reflective of the path so jobs/1/questions/1/answers/new(EDIT: this should be jobs/1/answers/new
) would show all of the questions with a job_id
of 1.
How do I go about doing this? I was thinking of using a new action like this in the answers controller ( which i'm positive is very wrong):
def new
@answer = Answer.new
10.times do
@job = Job.find(params[:job_id])
@question = @job.questions.find(params[:question_id])
@answer = @question.answers.build(params[:answer])
end
end
And here is my current answers form:
<%= form_for(@answer, :url => job_question_answers_path(@job, @question)) do |f| %>
<% f.fields_for :answers do |builder| %>
<%= builder.label @question.question %>
<%= builder.text_area :answer, :rows => 10 %>
<% end %>
<%= f.submit "Create" %>
<% end %>
Let me know if you need any more info and thanks for the help!