Reputation: 743
I am trying to make a little app where users can sign up, login, and be able to view and interact with questions for educational purposes.
I can visualize everything, but I'm having trouble translating that into actual code.
I know that a question model will have
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
Naturally, I know the strong_params will have to accept these attributes (parameters?) as well.
How can I make a model where the new-question.html.erb form will pass an array of 5 options, with the ability to mark one as correct? On top of this, I would like to shuffle or randomize the answer choices on each page load.
Any help or guidance would be helpful. Michael Hartl's tutorial is great, but I'm not sure if i'm missing things from it or things aren't clicking.
Upvotes: 0
Views: 326
Reputation: 14887
If the number of answers is always 5 or less, there's nothing wrong using a Question model with 5 text fields for the answers. You can also defaulting the first answer is the correct one and in the view showing the question and answers shuffle the answers.
rails g model Question title:text correct_answer:text answer_1:text answer_2:text ...
You're just getting started so don't bother too much with separate model for Question, Answer and nested form. Keep it simple.
Upvotes: 1
Reputation: 6253
sample for database schema
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
sample model relationship, there are 4 models, 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
for user to choose you can use radio_button_tag, here is link to learn
Upvotes: 1