How do I convert a simple ruby flashcard program into a ROR app?
Posted
by Mark Wilbur
on Stack Overflow
See other posts from Stack Overflow
or by Mark Wilbur
Published on 2010-05-11T16:54:13Z
Indexed on
2010/05/11
17:04 UTC
Read the original article
Hit count: 218
What I'm trying to do is make a basic flashcard app on rails. At this point, all I'm looking for is the functionality to iterate through a list of flashcards, quiz the user and let the user know if they were right or not. In ruby, it didn't take me long to write:
class Card
attr_accessor :answer, :question
def initialize(answer = "", question="")
@answer = answer
@question = question
end
def quiz
puts "What does #@question mean?"
answer = gets.chomp
if answer == @answer
puts "Right"
return true
else
puts "Wrong"
return answer
end
end
end
class Cardlist
attr_accessor :Cards
def initialize(Cards = [])
@Cards = Cards
end
def quiz
Cards.each do |w|
w.quiz
end
end
end
The problem I'm having with rails is figuring out where to put the logic to loop through all the cards in the list. I've made models specifying that Card belongs_to cardlist and that Cardlist has_many cards. I know application logic should go in the controller, but if I were to make a "quiz" action for my Cardlist controller, how would I make it iterate through all the cards? After each "quiz" page generated, I'd need to get an answer back from the user, respond (maybe flash) whether it was right or not and then continue onto the next question. Would any of that logic have to go into the view in order to make sure it's sending back the user inputted answers to the controller?
© Stack Overflow or respective owner