Rails form_for loop with nested resources
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!
So I figured out how to get this working and I did it using the railscasts episode:http://railscasts.com/episodes/196-nested-model-form-part-1.
I was aware of this fix to begin with, but I held back from using it because the function of my app is to allow the admins to create jobs and questions (behind a wall) and then have the users answer those respective questions. I figured that if I nested the answers form under the job and questions form, I wouldn't be able to create this admin/user functionality (that is my next feat).
I basically just nested the answers form under the questions form on the job/new action. It looks like this:
<%= form_for(@job) do |f| %>
<%= f.label :name %><br />
<%= f.text_field :name %>
<%= f.fields_for :questions do |builder| %>
<%= render 'question_fields', :f => builder %>
<% end %>
<%= f.submit %>
<% end %>
With the question partial being:
<%= f.label :question, "Question" %>
<%= f.text_area :question, :rows => 10 %>
<%= f.check_box :_destroy %>
<%= f.label :_destroy, "Remove Question" %>
<%= f.fields_for :answers do |builder| %>
<%= render 'partials/answer_fields', :f => builder %>
<% end %>
And the answers partial being:
<%= f.label "Answer" %>
<%= f.text_area :answer, :rows => 10 %>
<%= f.hidden_field :question_id, :value => @question %>
<%= f.hidden_field :job_id, :value => @job.id %>
I had to make a change in the Jobs#new method:
def new
@job = Job.new
10.times do
question = @job.questions.build
1.times { question.answers.build }
end
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @job }
end
end
Lastly my associations in the models needed to include
accepts_nested_attributes_for
Like so:
Models
class Job < ActiveRecord::Base
has_many :questions
has_many :answers
accepts_nested_attributes_for :questions, :allow_destroy => true
accepts_nested_attributes_for :answers, :allow_destroy => true
end
class Question < ActiveRecord::Base
belongs_to :job
has_many :answers
accepts_nested_attributes_for :answers
end
class Answer < ActiveRecord::Base
belongs_to :job
belongs_to :question
end
This solution is saving my answers but now I need to use that partial in another view to fulfill the functionality that I was attempting with the admin/user actions.
Let me know if you have any questions.
精彩评论