Skip to content

Instantly share code, notes, and snippets.

@jachenry
Created October 18, 2012 02:13
Show Gist options
  • Save jachenry/3909487 to your computer and use it in GitHub Desktop.
Save jachenry/3909487 to your computer and use it in GitHub Desktop.
Hierarchical Nested Form
<%= f.text_area :label, :rows => 2%>
<%= field_set_tag 'Answers' do %>
<%= f.fields_for :answers do |af| %>
<%= af.text_field :label %>
<%= af.collection_select(:followup_id, f.object.children, :id, :label, :include_blank => true) %>
<%= af.link_to_remove 'X', :class => 'radius button'%>
<% end %>
<%= f.link_to_add "Add Answer", :answers%>
<% end %>
<%= nested_form_for [@survey, @question] do |f| %>
<%= render :partial => "child_fields", :locals => {:f => f} %>
<%= field_set_tag 'Follow-up' do %>
<%= f.fields_for :children %>
<%= f.link_to_add "Add Followup", :children, :class => 'radius button two'%>
<% end %>
<%= f.submit %>
<% end %>
<div id="answers_fields_blueprint" style="display: none"><div class="fields">
<input id="question_answers_attributes_new_answers_label" name="question[answers_attributes][new_answers][label]" size="30" type="text">
<select id="question_answers_attributes_new_answers_followup_id" name="question[answers_attributes][new_answers][followup_id]"><option value=""></option>
<option value="38">Followup Question 2</option></select>
<input id="question_answers_attributes_new_answers__destroy" name="question[answers_attributes][new_answers][_destroy]" type="hidden" value="false">
<a href="javascript:void(0)" class="radius button remove_nested_fields" data-association="answers">X</a>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment