Ruby on rails Rails 5:如何将父资源的属性传递给嵌套资源?
我有一个模型,Ruby on rails Rails 5:如何将父资源的属性传递给嵌套资源?,ruby-on-rails,Ruby On Rails,我有一个模型,multipleechoicequestion,它有五个属性:answer\u one,answer\u two,answer\u four和answer\u five,以及一个名为answer\u correct的字段 我想跟踪每个用户在加载每个问题后单击后选择的内容。可能使用remote:true-我一直在互联网上阅读,创建一个名为UserAnswer的嵌套多态模型来跟踪用户对每个问题的答案选择似乎是一件明智的事情 然而,我所困惑的是如何将原始模型的参数(MultipleCho
multipleechoicequestion
,它有五个属性:answer\u one
,answer\u two
,answer\u four
和answer\u five
,以及一个名为answer\u correct
的字段
我想跟踪每个用户在加载每个问题后单击后选择的内容。可能使用remote:true-我一直在互联网上阅读,创建一个名为UserAnswer
的嵌套多态模型来跟踪用户对每个问题的答案选择似乎是一件明智的事情
然而,我所困惑的是如何将原始模型的参数(MultipleChoiceQuestion)传递给视图中的新模型,因为我的理解是所有模型在数据库中都有不同的属性。(当信息字符串来自父模型时,我如何确保用户通过点击二级模型选择的内容能够持久化?)
是否可以将父模型的属性传递给嵌套模型?这里的目的是让用户看到随着时间的推移他们得到的是对还是错
multiplechicequestion.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
#==架构信息
#
#表名:多项选择题
#
#id:bigint(8)不为空,主键
#问题:案文
#答案一:文本
#答复二:文本
#答复三:文本
#答复四:文本
#答案正确:文本
#答复(解释):文本
#已发布:布尔值
#标记:布尔值
#用户id:bigint(8)
#创建时间:datetime非空
#更新时间:datetime非空
#slug:字符串不为空
#资料来源:string
#评论:布尔
#谁被审查:字符串
#查看时间:datetime
#难度等级:整数
#多项选择\问题\分类\ id:整数
#争议:布尔默认值(FALSE)
#来源:整数默认值(“未给定”)
类MultipleChoiceQuestion
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
#==架构信息
#
#表名:用户
#
#id:bigint(8)不为空,主键
#电子邮件:字符串默认值(“”),不为空
#加密密码:字符串默认值(“”),不为空
#重置密码\u令牌:字符串
#重置密码发送时间:datetime
#还记得在:datetime创建的吗
#计数中的符号:整数默认值(0),不为空
#当前登录时间:datetime
#上次登录时间:datetime
#ip中的当前符号:字符串
#ip中的最后一个签名:字符串
#创建时间:datetime非空
#更新时间:datetime非空
#名字:string
#姓氏:string
#学校名称:string
#毕业年份:string
#当前\u符号\u在\u令牌中:字符串
#管理员:布尔默认值(FALSE)
#超级管理员:布尔默认值(FALSE)
#已验证:布尔默认值(FALSE)
#前置器:布尔默认值(FALSE)
#regularuser:布尔默认值(FALSE)
#banneduser:布尔默认值(FALSE)
#用户角色:布尔默认值(TRUE)
#用户名:字符串默认值(“”),不为空
类用户<应用程序记录
有很多:帖子,依赖::销毁
有很多:多项选择题,依赖::销毁
有很多:注释,依赖::销毁
有很多:国旗
有\u多个:已保存\u项
有没有附加一个:阿凡达
查看-多项选择问题/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>
答案选择#1:
答案选择#2:
答案选择#3:
答案选择#4:
正确答案:
以下是我要做的:
首先,你需要对你的考试或测验有一些参考。
第二,你的考试需要有x个问题。
第三,你的问题需要有x个可能的答案,其中一个答案是正确的。然后,获取用户模型,并将该用户与以下测试和答案关联
请注意,这不是有效的语法,但仅用于解释目的
class Exam has_many :questions
has_and_belongs_to_many :users
class Question has_many :answers
has_many :user_answers
belongs_to :exam
class Answer belongs_to :question
class UserAnswer belongs_to :question
belongs_to :user
class User has_and_belongs_to_many :exams
has_many :user_answes
现在,您可以将任何考试与任何用户关联,也可以将任何答案与任何用户关联
因此,例如,如果您向答案
控制器提交表单,则可以创建关联,跟踪每个用户先前选择的答案,如下所示:
answer = Answer.find(params[:id])
user_answer = UserAnswer.create(answer_id: answer.id, user_id: user.id, question_id: answer.question.id)
因此,现在您可以与您的用户
、考试
、和用户答案
模型进行比较,以获取用户针对特定考试给出的每个答案。如果您使用当前模型stru
#routes.rb
post '/verify_user_selected_answer', to: "multiple_choice_questions#verify_user_selected_answer'
<%= link_to @multiple_choice_question.answer_one, "javascript:void(0);", class: "answer" %>
$(document).ready(function() {
$("a.answer").on( "click", function( event ) {
var current_answer = $(this);
var question_id = '<%= @multiple_choice_question.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"]);
}
});
});
});
def verify_user_selected_answer
@multiple_choice_question = MultipleChoiceQuestion.find(params[:question])
@selected_answer = params[:answer]
@user = User.find(params[:user_id])
@multiple_choice_question.update_attribute(user_id, @user.id)
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