How do i Generate quiz from dropdown selection based on the subject choosen

    Template.home.events({
    'click  .quiz':  function (evt) {
     evt.preventDefault();
    var Id = this._id;
    var subject = Questions.findOne({_id: Id});
    var selected_subject = subject.selected_subject;
    console.log(selected_subject);
    if(selected_subject == 'Mathematics') {
        Questions.find({selected_subject: 'Mathematics'});
       Router.go('/quiz');
    }

  }

});

HTML:

         <div class="box-header with-border">
            <h3 class="box-title">Select Subject</h3><br><br>
            <select class="form-control select2" style="width: 100%;" id="selected_subject">
                <option selected="selected" >Physics</option>
                <option>Mathematics</option>
                <option>English</option>
                <option>Geography</option>
                <option>Physics</option>
                <option>Biology</option>
            </select><br>
         </div>

my Database is as follows:

 {
      "_id": "kAKbfLRHFZinrrRZv",
      "selected_subject": "English",
      "question": "what is the plural of Subject",
      "ans_A": "subjects",
      "ans_B": "book",
      "ans_C": "class",
     "ans_D": "courses",
    "correctAns": "subjects"
 }

Hi,

I’m a newbie to, but I’ll take a stab. As far as i can tell, you need to save the data from the user selection about the subject. Is that correct?

If so, it may be possible to:

  1. Save the user subject selection to a Session. Use Session.set(‘subjectSelection’, )
  2. Then, use Session.get to retrieve this information in the /quiz template.

I guess you will simply just use this to filter the publish of your database with a limit tag to get a subset of questions you can display. Just an idea. Thanks so much.

Tat

@tathagatbanerjee is correct. You need to do something with this data:

In fact, I’d suggest routing with the selected_subject as a route param:

Router.go('/quiz/' + selected_subject);

And then dealing with the data querying part once the quiz template is shown. E.g.

Template.quiz.helpers({
  questions: function () {
    var selected_subject = Router.current().params.selected_subject;
    return Questions.find({selected_subject: selected_subject});
  }
});

To make this work, you’ll need to change your route definition to:

Router.route('/quiz/:selected_subject', function () {
  // Whatever else you do in the route definition
});

The good thing about doing it this way, is you can route directly to a particular quiz.

2 Likes