Keyvan Sabras
Joined 2/19/2023
Keyvan Sabras said about 1 year ago on Gathering Questionnaire Responses :
 Hello David & everyone!
As a beginner on Rails, I've been following your tutorials so far to create my first survey.

Everything is going smoothly except for the nested-form controller, I can create questions & answers (multiple answers as well) but I can't delete a question or answer I've created.
I kept getting this error: 
Error invoking action "click->nested#remove_association" TypeError: t is null

Here is my nested-form controller : 
import { Controller } from "@hotwired/stimulus"
// Connects to data-controller="nested"
export default class extends Controller {  
static targets = ["add_item", "template"]  
static values = { index: String }

  add_association(event) {    
    event.preventDefault()
    var content = this.templateTarget.innerHTML.replace(new RegExp(this.indexValue, 'g'), new Date().valueOf())    
    this.add_itemTarget.insertAdjacentHTML('beforebegin', content)  }


  remove_association(event) {    
    event.preventDefault()    
    let item = event.target.closest(".nested-fields")    
    item.querySelector("input[body*='_destroy']").value = 1    
    item.style.display = 'none'  }}

I have just replaced the "name" by "body" in the remove_association, which is the name of my input in the questions & answers models.

If anyone has an idea how to correct this issue, or had this error it would be of great help!
Thanks a lot.


Keyvan Sabras said about 1 year ago on Gathering Questionnaire Responses :
The error seems to be located at line 18 of the controller :




Which corresponds to this line 

    item.querySelector("input[body*='_destroy']").value = 1

Here are also my form view : 
<%= simple_form_for ([@survey]) do |f| %>
  <%= f.input :name, label:"Survey name"%>
  <div data-controller='nested', data-nested-index-value='QUESTION_RECORD'>
    <template data-nested-target="template">
      <%= f.simple_fields_for :questions, Question.new, child_index: 'QUESTION_RECORD' do |question| %>
        <%= render "question_fields", f: question %>
      <% end %>
    </template>

    <%= f.simple_fields_for :questions do |question| %>
      <%= render "question_fields", f: question %>
    <% end %>

    <!-- Inserted elements will be injected before that target. -->
    <div data-nested-target="add_item">
      <%= f.button :submit, "Add question", data: {action:"nested#add_association"} %>
    </div>
  </div>
  <%= f.submit 'Create survey' %>
<% end %>


_question_fields.html.erb : 
<div class="nested-wrapper" data-new-record="<%= f.object.new_record? %>" data-controller='dynamic-select'>
  <%= f.input :category do %>
    <% f.select :category, Question.category_select, selected: f.object.category,
    'data-dynamic-select-target': 'select',
    'data-action': 'dynamic-select#selected' %>
  <% end %>
  <%= f.input :body, label:"Question" %>
  <%= f.button :submit, "Delete question", data: {action:"nested#remove_association"} %>
  <%= f.hidden_field :_destroy %>

  <div data-controller='nested', data-nested-index-value='ANSWER_RECORD' data-dynamic-select-target='choice'>
    <template data-nested-target="template">
      <%= f.simple_fields_for :answers, Answer.new, child_index: 'ANSWER_RECORD' do |answer| %>
        <%= render "answer_fields", f: answer %>
      <% end %>
    </template>

    <%= f.simple_fields_for :answers do |answer| %>
      <%= render "answer_fields", f: answer %>
    <% end %>

    <div data-nested-target="add_item">
        <%= f.button :submit, "Add answer", data: {action:"nested#add_association"} %>
    </div>
  </div>
  <div data-controller="nested-form" data-dynamic-select-target='long'>
  </div>
</div>

_answer_fields.html.erb : 
<div class="nested-wrapper" data-new-record="<%= f.object.new_record? %>">
  <%= f.hidden_field :id %>
  <%= f.input :body, label:"Answer" %>
  <%= f.button :submit, "Delete answer", data: {action:"nested#remove_association"} %>
  <%= f.hidden_field :_destroy %>
</div>

Keyvan Sabras said about 1 year ago on Gathering Questionnaire Responses :
Okay I found out, my div class was "nested-wrapper" and I was targeting ".nested-fields" in  my Stimulus Controller!
Thanks a lot for your help David!