views:

74

answers:

1

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 these two models:

class Card < ActiveRecord::Base
  belongs_to :cardlist
end

and

class Cardlist < ActiveRecord::Base
  has_many :cards
end

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? How do I send back information to the controller without writing it to the DB?

+1  A: 

IMO, a CardList instance is initialized and stored in the session when the user starts the quiz (you could also give the user the option to restart, which would be another method on the existing/another controller). So the CardList would be sitting in the session on the user and you would, on each quiz, give 'em another Card.

So your "each" method would divided up between web hits, if you will.

So your view displays the Card to the user, who answers and hits the method on the controller, where they get moved to the next Card and shown the view again. Using the flash variable is a fine way to give feedback to the user about the preceding commit.

As you've done, your card would typically be an ActiveRecord model. You made your CardList an ActiveRecord model too, which is a fine way to go.

Please note that you might want to use some different terms for quiz and quiz (like QuizSet?) to keep things a bit clearer.

Yar
I guess I should have written a bit more detail above. The Card and Card list are both from ActiveRecord.I'm not sure I fully understand the third paragraph. Would the user's answer go to the same method on the controller that generated the quiz view? Or would I have to add another method? Also, how does one get input from a user without using a form that writes to the DB?The "view" for Card would just be one card, but the "view" for Cardlist would be maybe 30 cards. Since both controllers use the same names for "view" and "edit", I thought doing the same for quiz was easier.
Mark Wilbur
@Mark Wilbur, while you might want to run through some tutorials on Rails, I'll answer briefly here. 1) Yes, the user's answer could post to the same method that generated the card view (you might just have one method in the controller). You check the params to see if it's a postback or the first time in. 2) Getting input from the user can be done using the `form_tag` in the view and `params` in the controller, which is a bit old school for rails. Cooler is a tableless model like this one http://codetunes.com/2008/07/20/tableless-models-in-rails/.
Yar
The scaffolding stuff (and map.resources), while good for the admin interface, might be confusing for your user's side of things. There you might want to use a normal controller with actions.
Yar
Thanks, yar. I've been going through all the tutorials I can find! Railscasts, Headsfirst book, stuff that I find on google, etc. So far, the tutorials I've seen all seem to be aimed at simple DB driven ecommerce stuff or blog applications. I don't think I've seen any that involved generating multiple screens iteratively from the controller or any form input that didn't go straight into the database. If I could find any tutorials with quiz apps or simple game apps I'd be all over them!I can't understand that link in isolation, but I really do appreciate your time and suggestions.
Mark Wilbur
@Mark Wilbur, you use a Tableless model exactly as if it were an ActiveRecord model -- so you can do a `form_for` with it in your view, for instance, and you can do a `MyTblessModel.new(params[:whatever])` within the controller too. In your app you could validate to see if the user put in an answer at all, and then reshow the card.
Yar