Adding multiple and selectable answers to a question in Ruby On Rails

0

Currently I have a survey in which I can perform short, long and numerical answers, however I have certain doubts when implementing multiple selectable answers, that is, the user instead of writing in a field will select a response within from a list, either by means of radio buttons or% select , for this I have the Survey , the Survey has many Questions , and each Question has many QuestionType , which are the type of question that is: Short text, Paragraph, Numeric, Multiple options, Drop-down list, etc.

So that multiple options or folding lists can exist, the options to be selected must be created in the creation form, but for the QuestionType , I have created another table called AnswerOption , which would logically believe that this must be linked to QuestionType , since QuestionType should have many AnswerOption , to later be able to nest them, am I correct?

However when it comes to printing the QuestionType , I need that when selecting Multiple options or Drop-down list a field appears to write the AnswerOption , there is no problem there, my problem lies at the time of storing, since they should nest at QuestionType , I appreciate an orientation on this, annex my migrations to have a better view of the problem:

survey.rb

class CreateSurveys < ActiveRecord::Migration[5.2]
  def change
    create_table :surveys do |t|
      t.string :title

      t.timestamps
    end
  end
end

question.rb

class CreateQuestions < ActiveRecord::Migration[5.2]
  def change
    create_table :questions do |t|
      t.string :title
      t.references :survey, foreign_key: true
      t.integer :question_type_id, foreign_key: true

      t.timestamps
    end
  end
end

answer_option.rb

class CreateAnswerOptions < ActiveRecord::Migration[5.2]
  def change
    create_table :answer_options do |t|
      t.text :title
      t.integer :question_type_id

      t.timestamps
    end
  end
end

form.html.erb

<%= form_with(model: survey, local: true) do |form| %>

    <!-- Form -->
      <div class="grid-x grid-margin-x">

        <div class="cell large-8">
          <%= form.label :nombre_de_la_encuesta %>
          <%= form.text_field :title, placeholder: "Ej. Diagnostico empresarial" %>      
        </div>

        <div class="cell large-12">
          <ol id="questions-fields">
            <%= form.fields_for :questions do |question| %>
              <%= render "question_fields", f: question %>
            <% end %>                 
          </ol>
        </div>

        <div class="cell large-12 text-right">
          <%= link_to surveys_path, class: "button alert margin-bottom-0" do %><i class="far fa-times"></i> Cancelar<% end %>  
          <%= button_tag(class: "button margin-bottom-0") do %><i class="far fa-check"></i> Hecho<% end %>
        </div>    

      </div>
    <!-- End Form -->

  </div>
</div>

<% end %>

_question_fields.html.erb

<li>
    <%= f.label :pregunta %>
    <div class="grid-x grid-margin-x">
        <div class="cell large-6">
            <%= f.text_field :title, placeholder: "Ej. ¿Cuál es el nombre de su empresa?" %>            
        </div>
        <div class="cell large-3">
            <%= f.collection_select(:question_category_id, QuestionCategory.all, :id, :title, prompt: "Categoria") %>        
        </div>
        <div class="cell large-3">
            <%= f.collection_select(:question_type_id, QuestionType.all, :id, :title, prompt: "Tipo de pregunta") %>

            <!-- El problema se concentraria basicamente aqui, en donde deberia poder crear las AnswerOption --> 

        </div>
    </div>
</li>
    
asked by Hector Hernandez 18.12.2018 в 20:42
source

0 answers