Cheers! I am trying to store the social network response suboptions, as seen in the image, however for some reason it is not being stored, the server does not record the storage of the "subrespondent" field, the idea is that it takes the selected options in the boxes and put them in a single array that will be stored inside the "subrespondent" field, probably, despite having established the array, I think something is missing to join the data and store it in the field, I appreciate your help
form.html.erb
<%= form_with(model: respondent, local: true) do |form| %>
<ol>
<% @survey.questions.each do |q| %>
<li>
<%= form.fields_for :answers do |a| %>
<%= q.title %>
<ol type="none">
<% q.question_options.each do |qo| %>
<li>
<%= a.radio_button :response, qo.id %>
<%= a.label :response, qo.title %>
<%= a.hidden_field :question_id, value: q.id %>
<ol type="none">
<% qo.sub_question_options.each do |sqo| %>
<li>
<%= a.check_box :subresponse, value: sqo.id %>
<%= a.label :subresponse, sqo.title %>
<%= a.hidden_field :question_option_id, value: sqo.id %>
</li>
<% end %>
</ol>
</li>
<% end %>
</ol>
<% end %>
</li>
<% end %>
</ol>
<% end %>
respondents_controller.rb
class RespondentsController < ApplicationController
def new
@survey = Survey.find(params[:survey])
@respondent = Respondent.new
@respondent.answers.build
end
def respondent_params
params.require(:respondent).permit(:name, :email, :phone, :survey_id, answers_attributes: [:id, :response, :subresponse, :question_id, :question_option_id, :respondent_id])
end
end
create_answer.rb
class CreateAnswers < ActiveRecord::Migration[5.2]
def change
create_table :answers do |t|
t.text :response
t.text :subresponse, array: true, default: []
t.references :question, foreign_key: true
t.integer :question_option_id, foreign_key: true
t.references :respondent, foreign_key: true
t.timestamps
end
end
end
General structure of the project
I assume that by "subrespondent" you mean "subresponse".
I'm not sure, but I think your error is because you're not accepting the subresponse parameter as an array:
Also as you are using your checkbox it is not the correct way . Test:
sqo.id
defines the value when it goes checked andnil
for when it goes unchecked. If you don't define this, rails will default to ones and zeros to indicate whether to go checked/unchecked.And I insist, if this does not solve it, add the definition of your method and show the log of when you make the request that does not save the information.