Stuck on frontend design for my Ruby quiz app
I’ve got a cool backend set up for my quiz app using Ruby. It’s got models for Quiz, QuizQuestion, and QuizChoice. The controller is called ‘quizzes’ and it’s working great!
Now I’m trying to figure out the frontend part. I need to:
- Keep track of quiz scores
- Remember which quiz a user took
- Log correct and incorrect answers
I’m thinking about these models:
class QuizResult
belongs_to :quiz
belongs_to :user
has_many :question_results
attribute :score, :integer
end
class QuestionResult
belongs_to :quiz_question
belongs_to :quiz_result
attribute :correct, :boolean
end
But I’m confused about how to make a RESTful controller for this. Any ideas on how to approach this? I want to keep it clean and follow best practices. Thanks!
hey zack, ur models look good! for the controller, maybe try a QuizSessionsController? it could handle startin quizzes, savin answers, and showin results. sumthin like:
class QuizSessionsController < ApplicationController
def create
# start quiz
end
def update
# save answers
end
def show
# display results
end
end
this keeps it simple and restful. hope it helps!
Your approach with QuizResult and QuestionResult models looks solid. For the RESTful controller, consider creating a QuizAttemptsController. This would handle the creation and submission of quiz attempts.
Here’s a potential structure:
class QuizAttemptsController < ApplicationController
def create
@quiz_attempt = QuizResult.new(quiz: Quiz.find(params[:quiz_id]), user: current_user)
# Initialize attempt
end
def update
@quiz_attempt = QuizResult.find(params[:id])
# Update attempt with answers
end
def show
@quiz_attempt = QuizResult.find(params[:id])
# Display results
end
end
This structure allows you to create an attempt, update it as the user progresses through the quiz, and show the final results. Remember to implement proper authentication and authorization to ensure users can only access their own attempts. Also, consider adding an index action to list all attempts for a user.