我有一个有效的实现,我有一个父资源MultipleChoiceQuestion
,称为 aUser
可以有很多。
MultipleChoiceQuestion
有几个属性,从question
(即问题本身)开始,以及它的答案选择属性,即answer_one
, answer_two
, answer_three
, answer_four
,answer_five
和answer_correct
我需要能够将问题与所有答案选项一起显示,并让用户选择一个答案,将其保留,并允许他们查看他们回答对或错的问题。
我在想必须制作一个新模型UserAnswer
,但我不确定它的属性应该是什么,考虑到它将属于MultipleChoiceQuestion
- 以及如何将参数传递给这个新模型是我被卡住的上。
以下是相关代码:
MultipleChoiceQuestions 控制器
def tagged
@mcqs = MultipleChoiceQuestion.with_tag(params[:tag]).order("created_at DESC").paginate(:page => params[:page], :per_page => 1)
authorize @mcqs
end
def verify_user_selected_answer
@multiple_choice_question = MultipleChoiceQuestion.find(params[:question])
@selected_answer = params[:answer]
@user_id_who_selceted = params[:current_user]
if @selected_answer.downcase == @multiple_choice_question.answer_correct.downcase
@result = "Correct answer!"
else
@result = "Wrong answer!"
end
respond_to do |format|
format.json { render json: { result: @result } }
end
end
标记的.html.erb
<% @mcqs.each do |mcq| %>
<% mcq.answers.each do |answer| %>
<div class="gr-question--choice">
<%= link_to answer, "javascript:void(0);", class: "answer" %>
</div>
<% end %>
<script>
$(document).ready(function() {
$("a.answer").on( "click", function( event ) {
var current_answer = $(this);
var question_id = '<%= mcq.id %>';
var current_user = "<%= current_user.id %>";
$.ajax({
url: "/verify_user_selected_answer",
type: "POST",
dataType: "json",
data: {user_id: current_user, question: question_id, answer: current_answer.text()},
success: function(response){
$("#display_result").text(response["result"]);
}
});
});
});
</script>
<% end %>
路线.rb
post '/verify_user_selected_answer', to: "multiple_choice_questions#verify_user_selected_answer"
在 Rails 控制台中,正在传递数据参数,这就是证明
Started POST "/verify_user_selected_answer" for 127.0.0.1 at 2018-10-19 16:15:16 +0500
Processing by MultipleChoiceQuestionsController#verify_user_selected_answer as JSON
Parameters: {"user_id"=>"1", "question"=>"77", "answer"=>"answer four"}
它具有尝试它的用户 ID、问题 ID 以及选择的答案。我需要在一个单独的模型中坚持这些尝试,但这必须通过 Ajax 来完成。
基本上,为了能够看到有多少人尝试了任何一个问题,谁选择了什么选择,并且用户自己应该能够随着时间的推移看到他们做对了哪些是错的,这就是我寻求帮助的原因- 谢谢! !
编辑:
多项选择题.rb
# == Schema Information
#
# Table name: multiple_choice_questions
#
# id :bigint(8) not null, primary key
# question :text
# answer_one :text
# answer_two :text
# answer_three :text
# answer_four :text
# answer_correct :text
# answer_explanation :text
# published :boolean
# flagged :boolean
# user_id :bigint(8)
# created_at :datetime not null
# updated_at :datetime not null
# slug :string not null
class MultipleChoiceQuestion < ApplicationRecord
belongs_to :user, optional: true
validates :user, presence: true
belongs_to :multiple_choice_question_classification, optional: true
has_many :flags, dependent: :destroy
acts_as_taggable
# activity feed
include PublicActivity::Model
tracked owner: Proc.new { |controller, model| controller.current_user ? controller.current_user : nil }
extend FriendlyId
friendly_id :question, use: %i(slugged history finders)
def should_generate_new_friendly_id? #will change the slug if the name changed
question_changed?
end
def answers
[answer_one, answer_two,
answer_three, answer_four,
answer_correct].shuffle
end
end
用户.rb
# == Schema Information
#
# Table name: users
#
# id :bigint(8) not null, primary key
# email :string default(""), not null
# encrypted_password :string default(""), not null
# reset_password_token :string
# reset_password_sent_at :datetime
# remember_created_at :datetime
# sign_in_count :integer default(0), not null
# current_sign_in_at :datetime
# last_sign_in_at :datetime
# current_sign_in_ip :string
# last_sign_in_ip :string
# created_at :datetime not null
# updated_at :datetime not null
# first_name :string
# last_name :string
# school_name :string
# graduation_year :string
# current_sign_in_token :string
# admin :boolean default(FALSE)
# superadmin :boolean default(FALSE)
# verified :boolean default(FALSE)
# premiumuser :boolean default(FALSE)
# banneduser :boolean default(FALSE)
# user_role :boolean default(TRUE)
# username :string default(""), not null
#
class User < ApplicationRecord
has_many :multiple_choice_questions, dependent: :destroy
has_many :comments, dependent: :destroy
has_many :verbal_questions
has_many :flags
has_many :saved_items
has_many :reviews
end