Saturday, 7 October 2017

Nested form

Question to answer


1. Gem file: 
          gem "nested_form"

 application.js:
         //= require jquery_nested_form


2. Question controller:
         params.require(:question).permit(:question_type, :body, answers_attributes: [:id, :question_id, :body, :_destroy])

3. Question.rb:
        has_many :answers, dependent: :destroy
        accepts_nested_attributes_for :answers, :allow_destroy => true

4. answer.rb:
       belongs_to :question

5. question form: 

     <table id="tasks">
    <%= form.fields_for :answers do |answer_form| %>
      <div class="row">
          <%= answer_form.text_area :body  ,class: "form-control mynewinput1", placeholder: "Question" %>
          <%= answer_form.link_to_remove "Remove", class: "btn btn-danger" %>
      </div>
    <% end %>
  </table>
<p><%= form.link_to_add "Add a Answer", :answers, :data => { :target => "#tasks" } %></p>

No comments:

Post a Comment