1
votes

I have three resources: Jobs, Questions and Answers. The relationships are: Job has many questions; Question has many Answers; Job has many Answers.

I have nested all of the forms on the jobs/new view.

Now the goal of the app is for the admins (us) to create jobs and questions behind an admin wall. Once we do that we want to list out the questions created for each specific job and have the users answer the questions. This requires putting the answers form on another view (same or different controller) that is not behind an admin wall.

Since the forms are all nested on the jobs/new view, I created a partial for the answers form:

 <%= 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 %>

My thinking was that I create a partial and I will be able to reference it wherever I want in the app, but I have been trying everything and I can't seem to get it to work.

I basically have 2 questions out of this setup:

1) How do I go about rendering the answers partial by itself on another view (what is the correct code)? 2) Where is the best place to create this view? My initial thinking was another jobs view since it will be nested under the parent resource, but i'm not entirely sure if this works.

Thanks and let me know if you need any more info or clarification.

2

2 Answers

2
votes

Try following

   <%= render :partial=> '/partials/answer_fields', :f => builder %>

Instead of

   <%= render 'partials/answer_fields', :f => builder %>
2
votes

How about keep the same structure you have here, but do not render out any fields associated with the Job and Question models? This way the nesting will still be passed to the controller and you don't have to change that functionality.

<%= form_for(@job) do |f| %>
  <%= f.fields_for :questions do |q| %>
    <%= q.fields_for :answers do |a| %>
      <%= render 'partials/answer_fields', :f => a %>
    <% end %>
  <% end %>
<% end %>

Would that work for you?