I come with a problem mainly of logic, I am developing a survey, which works perfectly, your question is created, and you get the answer, all right, but the field that gets the answer only accepts as data type " string ", and there are questions that need to be answered numerically, and others that are Boolean, because the response field is in a loop, all get the same type of input, let's say that there would be no problem here since I can specify the type of response that could be obtained through the view by means of a condition, show the required input type [text, number, select, etc. ], however my problem is found in the type of data that the table field will receive in the database, since by default it is "string", but if the response sent is a Boolean? A float? Should I create multiple fields in the table that receive different types of data? Or in what way could you handle this situation? Finally, at the moment I can answer the questions, I show them in the show view of the survey that was just created, and I add the answer form below each question by means of a loop, but I have to send one by Once, when you should have a single submit for the whole survey, attach my code:
show.html.erb (Survey)
<p>
<strong>Name:</strong>
<%= @survey.name %>
</p>
<ol>
<% @survey.questions.each do |question| %>
<li>
<%= question.survey_question %>
<%= form_with(model: [ question, question.answers.build ], local: true) do |form| %>
<p>
<%= form.label :survey_answer %><br>
<%= form.text_area :survey_answer %>
</p>
<p>
<%= form.submit %>
</p>
<% end %>
</li>
<br>
<% end %>
</ol>
form.html.erb (Survey)
<%= form_with(model: survey, local: true) do |form| %>
<% if survey.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(survey.errors.count, "error") %> prohibited this survey from being saved:</h2>
<ul>
<% survey.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :name %>
<%= form.text_field :name %>
</div>
<ol>
<%= form.fields_for :questions do |builder| %>
<%= render "question_fields", form: builder %> <br>
<% end %>
</ol>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
question_fields.html.erb
<li>
<%= form.label :pregunta %>
<%= form.text_area :survey_question %>
<%= form.check_box :_destroy %>
</li>
question.rb (Model)
class Question < ApplicationRecord
belongs_to :survey
has_many :answers
accepts_nested_attributes_for :answers, reject_if: proc { |attributes| attributes['survey_response'].blank? }, allow_destroy: true
end
survey.rb (Model)
class Survey < ApplicationRecord
has_many :questions
accepts_nested_attributes_for :questions, reject_if: proc { |attributes| attributes['survey_question'].blank? }, allow_destroy: true
end
surveys_controller.rb
class SurveysController < ApplicationController
before_action :set_survey, only: [:show, :edit, :update, :destroy]
def new
@survey = Survey.new
question = @survey.questions.build
question.answers.build
end
def edit
question = @survey.questions.build
question.answers.build
end
private
def set_survey
@survey = Survey.find(params[:id])
end
def survey_params
params.require(:survey).permit(:name, :user_id, questions_attributes: [:id, :survey_question, :survey_id, :_destroy])
end
end
answer.rb (Migration)
class CreateAnswers < ActiveRecord::Migration[5.2]
def change
create_table :answers do |t|
t.string :survey_answer
t.integer :question_id
t.timestamps
end
end
end
question.rb (Migration)
class CreateQuestions < ActiveRecord::Migration[5.2]
def change
create_table :questions do |t|
t.string :survey_question
t.integer :survey_id
t.timestamps
end
end
end
survey.rb
class CreateSurveys < ActiveRecord::Migration[5.2]
def change
create_table :surveys do |t|
t.string :name
t.timestamps
end
end
end
routes.rb
Rails.application.routes.draw do
resources :surveys do
resources :questions
end
resources :questions do
resources :answers
end
end