【问题标题】:How to model and integrate a rails mutliple choice quiz?如何建模和集成 Rails 多项选择测验?
【发布时间】:2023-03-24 16:20:01
【问题描述】:

我正在尝试制作一个小应用程序,用户可以在其中注册、登录,并能够出于教育目的查看问题并与之互动。

我可以将所有内容可视化,但我无法将其转换为实际代码。

我知道问题模型会有

Question Title - as a string or text

Question Answer 1 - as a string or text

Question Answer 2 - as a string or text

Question Answer 3 - as a string or text

Question Answer 4 - as a string or text

Question CORRECT ANSWER 5 - as a string or text

当然,我知道 strong_params 也必须接受这些属性(参数?)。

如何制作一个模型,其中 new-question.html.erb 表单将传递 5 个选项的数组,并能够将其中一个标记为正确?最重要的是,我想在每个页面加载时随机化或随机化答案选择。

任何帮助或指导都会有所帮助。 Michael Hartl 的教程很棒,但我不确定我是否遗漏了其中的内容或没有点击。

【问题讨论】:

    标签: ruby-on-rails forms model-view-controller database-design


    【解决方案1】:

    数据库架构示例

      create_table "questions", force: :cascade do |t|
        t.references "quiz_id"
        t.string   "question_word"
        t.string   "option1"
        t.string   "option2"
        t.string   "option3"
        t.string   "option4"
        t.integer  "answer",            default: 0
        t.integer  "score",             default: 2
        t.datetime "created_at",                    null: false
        t.datetime "updated_at",                    null: false
      end
    
      create_table "quizs", force: :cascade do |t|
        t.string   "quiz_name"
        t.string   "notes"
        t.datetime "created_at",                    null: false
        t.datetime "updated_at",                    null: false
      end
    
      create_table "user_quiz", force: :cascade do |t|
        t.references  "user_id"
        t.references  "quiz_id"
        t.integer  "score",          default: 0
      end
    

    样本模型关系,有4个模型,User, Quiz, Question, UserQuiz

      class Quiz < ActiveRecord::Base
        has_many   :questions
        has_many   :user_quizs
        has_many   :users, :through   => :user_quizs
      end
    
      class User < ActiveRecord::Base
        has_many   :user_quizs
        has_many   :quizs, :through   => :user_quizs
      end
    
      class Question < ActiveRecord::Base
        belongs_to :quiz
      end
    
      class UserQuiz  < ActiveRecord::Base
        belongs_to :user
        belongs_to :quiz       
      end
    

    用户可以使用radio_button_tag进行选择,这里是link to learn

    【讨论】:

      【解决方案2】:

      如果答案的数量始终为 5 个或更少,则使用带有 5 个文本字段的问题模型作为答案没有任何问题。您也可以默认第一个答案是正确的,并在显示问题和答案的视图中随机播放答案。

      rails g model Question title:text correct_answer:text answer_1:text answer_2:text ...
      

      您才刚刚开始,所以不要为问题、答案和嵌套表单的单独模型而烦恼太多。保持简单。

      【讨论】:

      • 默认第一个答案是什么意思?理想情况下,用户可以单击“显示答案”按钮,在该按钮上可以显示正确答案,这将如何影响?谢谢。
      • 我的意思是假设正确答案是第一个,所以你不需要额外的字段来记录哪个答案是正确的。我更新了答案以使其更清晰。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-05
      • 2016-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多