asjer said about 2 years ago on Gathering Questionnaire Responses :
How would you go about turning this into something like an exam?

David Kimura PRO said about 2 years ago on Gathering Questionnaire Responses :
  I would start by having an attribute on the response that is tied to the user/student (user:belongs_to) and put a validation uniqueness scope on the response which scopes the user and questionnaire. This would limit the user for one response per questionnaire/exam. What other kind of functionality are you looking for?

A couple of thoughts, 

  • when displaying out the answers when gathering the response, you could shuffle the array of answers so they do not appear in the same order for everyone.
  • the creator of the exam could mark an answer as correct on each question. when a response is submitted, a background job can be queued up to grade the questionnaire response.
  • A grading table which has user:belongs_to, questionnaire:belongs_to, and response:belongs_to along with grade could be created as a result. This would increase the performance of the application as it doesn't have to do the grading calculations every time.

asjer said about 2 years ago on Gathering Questionnaire Responses :
Ah great thanks, sounds about right for what I need! 👌🏼

Only thing I'd want to add would be different questions weights, but would only require an extra column to the questions table.

David Kimura PRO said about 2 years ago on Gathering Questionnaire Responses :
Just some thoughts on weights... For the weights, I would recommend not trying to match 100%. It's not too difficult, but it's really annoying. So much front end validation to do and requiring the user to do some math to match up values to 100%. Instead, if you have 10 questions and you give them a weight of 50 each, then each question would be 10% of the total score. This will be much easier logic to handle. You could use a stimulus controller to add up each of the weight inputs and show a percentage value as you setup the questionnaire. 

Kam kara said 9 months ago on Gathering Questionnaire Responses :
Hello   David Kimura I try to rebuild this project after I want to integrate on my Start up like Exercise feature.
why did you choose the json format for answer? when you created Responses Scaffold, Why important?
I think json is for answers coming from APIs

David Kimura PRO said 9 months ago on Gathering Questionnaire Responses :
  Kam kara  I chose json format since the structure and format of the responses are not a consistent structure. Basically, it makes it more simple to store the data. A questionnaire could have a few questions and then many answers or many questions and just a few answers each. If you were to create a table-based response, then you would need to almost mimic the structure of the questionnaire where you have a responses table which has many question_responses and the question responses has many answer_responses (since it could me multiple choice). Then you need to worry about the type of response for the answer where the some of them may be a boolean, string, or text and picking the correct type when storing the response. 

Luke Scott said 8 months ago on Gathering Questionnaire Responses :
I've followed these videos to make a questionnaire system and now I want to be able to export the results as a csv.  I've watched the earlier video (EP 35), but can't get my head around how to go about this. What would be best?

Tom Murdoch said 8 months ago on Gathering Questionnaire Responses :
Great tutorial   David Kimura  ..... is there a way to go back and edit questionnaire responses after a submission? Thanks in advance 

David Kimura PRO said 8 months ago on Gathering Questionnaire Responses :
  Luke Scott I would make a Ruby class which took in the results of a questionnaire and format it in a way that you're wanting the CSV to look. So, you'd basically make an array with the first item in the array being an array of all of the headers. Then you'd loop through each response and put their response in the appropriate position in the array. From there, you can generate the CSV response and send a file to the client.

David Kimura PRO said 8 months ago on Gathering Questionnaire Responses :
  Tom Murdoch  If a questionnaire has already received responses, then I would not recommend altering the questionnaire. I've seen this before and it caused so many problems where the first several responses has the old questionnaire in mind but no longer valid. While, as developers of the questionnaire feature, we would know how to navigate this "safely", but end users will always surprise you with what they'd do. The safest thing would be to recall a questionnaire and generate a copy of an existing one to then modify.

Tom Murdoch said 8 months ago on Gathering Questionnaire Responses :
Hi David, thanks for the fast response! I do not wish to alter the questionnaire questions or associated answers. The questions/answers will never change in my particular case. I would however like the ability for the user to be able to edit their responses though. So they can go back and correct grammar and spelling if they need to in the future.   

officialklashed said 7 months ago on Gathering Questionnaire Responses :
I have two questions  David Kimura  

1) How do I link to the response show page from questionnaire index page?

I tried
<%= link_to pluralize(questionnaire.responses.count, "Response"), questionnaire_response_path(questionnaire, @response) %>

I got error
No route matches {:action=>"show", :controller=>"responses", :id=>nil, :questionnaire_id=>"1"}, missing required keys: [:id]

2) How do I make this questionnaire available for student and lecturer without having to write double code? The difference between the lecturer and student questionnaire is obviously the questions asked.

I can share my github link for help.

David Kimura PRO said 7 months ago on Gathering Questionnaire Responses :
1. The response instance variable looks to be nil in the snippet which is why you probably cannot link to the response show page. 

2. When are you trying to make the differentiation? Is it when pulling up a list of available questionnaires to take? If so, you could have an attribute on the questionnaire for the type of user. Something like "responder_type" and make it an enum. When listing out the questionnaires, you could query on this attribute depending on the actor (a lecturer or student).

Keyvan Sabras said 3 months 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.


David Kimura PRO said 3 months ago on Gathering Questionnaire Responses :
I haven't come across that error, but if I had to guess, it could be something to do with this line

event.target.closest(".nested-fields")    

And it not being found. Do you have any more info as to where the error is located?

Keyvan Sabras said 3 months 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>

David Kimura PRO said 3 months ago on Gathering Questionnaire Responses :
In these kinds of situations, I'd inspect the HTML elements to see what's getting rendered as it could be a bit different when using SImpleForm.

I would also hop into the browser console to try and target the element to see if I can retrieve it.

document.querySelector("input[name*='_destroy']")

Keyvan Sabras said 3 months 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!

Login to Comment