Home > database >  Store User_choice related to question_id and user_choice
Store User_choice related to question_id and user_choice

Time:06-27

Hope you all are well.

I am creating a mcqs rails application. I have created the question model in which i have this.

```class CreateQuestions < ActiveRecord::Migration[6.1]
  def change
    create_table :questions do |t|
      t.text :question
      t.text :option1
      t.text :option2
      t.text :option3
      t.text :option4
      t.text :answer

      t.timestamps
    end
  end
end```

Then i create a result model in which i have this.

```class CreateResults < ActiveRecord::Migration[6.1]
  def change
    create_table :results do |t|
      t.text :user_choice
      t.integer :q_id
      t.integer :u_id

      t.timestamps
    end
  end
end```

I make question_id and User_id foreign key in this table.

Now i want to store user_choice compare it with answer and show result to the user that you have enter right or wrong answer. But now i am unable to make such this. I don't know what to do now.

I will be admire if anybody help me in this regard. I am kinda new to rails. Please help i am stuck here.

```class Result < ApplicationRecord

    validates :user_choice, presence: true
    belongs_to :user
    belongs_to :question

end```

This is result model.

This is my question#index view i want to store user_choice from this to result table.


  <div >
    <div >
       <!-- Nested Row within Card Body -->
      <div >
          <div >
          <div >
       <h1 >Multiple-Choice Questions</h1>
        </div>
    <%= form_with model: @result, url:result_path do |form|%>
   <% @questions.each do |question| %>
    <div id="<%= dom_id(question) %>">
        <p><%= question.question %></p>
        
        <% choices = [question.option1, question.option2, question.option3, question.option4] %>
        <% choices.each do |c| %>
            <div>
                <%= radio_button_tag "guess_#{question.id}", c%>
                <%= label_tag "guess_#{question.id}_#{c}", c  %>
            </div>
        <% end %>
    </div>&nbsp;&nbsp;
<% end %>
       <div > <%= button_to "Submit", result_path, class:"btn btn-primary" %></div>
<% end %>
<div ><%= link_to 'New Question', new_question_path,class:"btn btn-primary btn-user" %>
</div>
            </div>
        </div>
      </div>
     </div>
  </div>````






CodePudding user response:

As per your table structure, you can add the following associations

class Result < ApplicationRecord
  validates :user_choice, presence: true
  belongs_to :user, foreign_key: :u_id, class_name: 'User'
  belongs_to :question, foreign_key: :q_id, class_name: 'Question'
end

And then you can compare the User answer as following

def is_answer_correct?
  question.answer == user_choice
end

I will also recommend moving the questions option into a separate table. This will make it easy to manage. You can refer to the DB structure added in this SO answer

View form

<div >
    <div >
       <!-- Nested Row within Card Body -->
      <div >
          <div >
          <div >
       <h1 >Multiple-Choice Questions</h1>
        </div>
    <%= form_with model: @result do |form|%>
   <% @questions.each do |question| %>
    <div id="<%= dom_id(question) %>">
        <%= f.hidden_field, :question_id, question.id %>
        <p><%= question.question %></p>
        
        <% choices = [question.option1, question.option2, question.option3, question.option4] %>
        <% choices.each do |c| %>
            <div>
                <%= form.radio_button :user_choice, c %>
                <%= form.label :user_choice, c  %>
            </div>
        <% end %>
    </div>&nbsp;&nbsp;
<% end %>
       <div > <%= form.submit "Submit", class:"btn btn-primary" %></div>
<% end %>
<div ><%= link_to 'New Question', new_question_path,class:"btn btn-primary btn-user" %>
</div>
            </div>
        </div>
      </div>
     </div>
  </div>
  • Related