Thursday, April 14, 2011

Agile Web Development with Rails

Ahh, it feels good to have a new coding book! Online tutorials and help from srs Rails guys is a fantastic resource, but a good book is absolutely invaluable.

I picked up a copy of Agile Web Development with Rails this week. I even sprang for a hard copy, despite my philosophy against owning physical media. Seriously, cavemen had books. Look it up. Anyway! I finally got a chance to sit down with it this evening. (Saying "finally" here is funny, since I just got it yesterday, but couldn't do anything with it since the Indy RB meetup was last night)

STILL, "finally" feels appropriate, considering I spent all day at work foaming at the mouth to get home and start coding. So far I really like the book. Like Beginning Ruby, it's written in an easy to follow, engaging manner with practical examples. Is there any other way to write a coding book? Probably not if you want people to read it. So far a lot of what I've seen in the book is refresher material for me, since I've spent the last couple of weeks doing Rails work by following online tutorials and working on Dungeon Roller.

Still, I've already learned some helpful things. Such as having two terminal windows open: one to run the Rails server of my project, and one to perform tasks. I had no idea! It simply did NOT occur to me that I could have two windows open. Another helpful tidbit came with generating a controller. If you write the code as such:

rails generate controller Say hello goodbye

Rails creates a controller named Say, with two empty methods inside of it: hello and goodbye. This definitely saves time later, because up until now I'd been writing methods by hand. Finally (and this is getting into code theory, but we're talking kiddie pool stuff here) I learned the idea that the controller holds the data and the view displays it. Pretty basic, but essentially even though you CAN write code directly into the view, it's a better idea to have the controller handle it. This idea is similar to separating raw HTML from styling.

On a side note, I've decided to rename Dungeon Roller to Manticore, both as a nod to Dwight from the Office and in homage to my favorite mythological creature.

Monday, April 11, 2011

Dungeon Roller progress

So I'm not entirely at a loss here. I've got the main Character model and three other models associated with Character: Statistic, Safe and Combat. Here's what the Character screen looks like:

Once you click through on a character, this is what you see:

Now say you want to click through on Statistics:

But here's the problem. The individual characters are all using the same instance of the Statistic model. I haven't worked with the other models yet, but I'm assuming this would be true for them as well. How do I need to edit the code so each Character has a separate Statistic, instead of the same Statistic being shared?

Here's my code from the routes.rb file:

DungeonRoller::Application.routes.draw do
  resources :combats

  resources :saves

  resources :statistics

  resources :characters do
    resources :combats
    resources :saves
    resources :statistics

And some code from the models:

class Character < ActiveRecord::Base   has_many :statistics   has_many :combats   has_many :saves end And each of the other models have: class Statistic < ActiveRecord::Base

  belongs_to :character


I thought associating these as has_many would solve the problem, but apparently I was wrong. Any input on this would be greatly appreciated!