我有一个模型,MultipleChoiceQuestion它有五个属性:answer_one、answer_two、和answer_three,以及一个名为answer_fouranswer_fiveanswer_correct
我想跟踪每个用户在加载每个问题后选择onClick的内容。可能使用 remote: true - 我一直在互联网上阅读,似乎创建一个名为的嵌套多态模型UserAnswer将是一件聪明的事情,以跟踪用户对每个问题的答案选择。
但是,我感到困惑的是如何将原始模型(MultipleChoiceQuestion)的参数传递给视图中的新模型,因为我的理解是所有模型在数据库中都有不同的属性。(当信息字符串来自父模型时,我如何确保我可以通过辅助模型保留用户通过点击选择的内容?)
是否可以将父模型的属性传递给嵌套模型?这里的目的是让用户随着时间的推移看到他们做对或错了。
MultipleChoiceQuestion.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
# source :string
# reviewed :boolean
# who_reviewed :string
# reviewed_at :datetime
# difficulty_rating :integer
# multiple_choice_question_classification_id :integer
# controversial :boolean default(FALSE)
# origination :integer default("not_given")
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 }
validates :question, :slug, presence: true, length: { minimum: 5 }
validates_uniqueness_of :question
User.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)
# regularuser :boolean default(FALSE)
# banneduser :boolean default(FALSE)
# user_role :boolean default(TRUE)
# username :string default(""), not null
class User < ApplicationRecord
has_many :posts, dependent: :destroy
has_many :multiple_choice_questions, dependent: :destroy
has_many :comments, dependent: :destroy
has_many :flags
has_many :saved_items
has_one_attached :avatar
查看 - multiple_choice_questions/show.html.erb
<h5>
<%= @multiple_choice_question.question %>
</h5>
<p>
<span>Answer choice #1:</span>
<%= @multiple_choice_question.answer_one %>
</p>
<p>
<span>Answer choice #2:</span>
<%= @multiple_choice_question.answer_two %>
</p>
<p>
<span>Answer choice #3:</span>
<%= @multiple_choice_question.answer_three %>
</p>
<p>
<span>Answer choice #4:</span>
<%= @multiple_choice_question.answer_four %>
</p>
<p class="correct">
<span>Correct Answer:</span>
<%= @multiple_choice_question.answer_correct %>
</p>