Rails Basics… Finally!
The sitepoint book arrived early this week and it moves much more closely to my pace. There are a few basic things that I’ve picked up both from this book and the Pragmatic book that I feel it’s time to document here.
Installation
First of all, for the installation of Rails, I decided to go with Instant Rails. I prefer for the set up process to be as painless as possible and Instant Rails makes this so. It has Rails, Ruby, a web server, and a database all wrapped up in one easy-to-install bundle. If you’re a more advanced user and would prefer to pick and choose your server or database application, then knock yourself out… but Instant Rails works just fine for me.
Command Line
Once you’re up and running, you quickly learn that a lot of Rails functionality can (and I guess should?) happen at the command line. This is a new concept for me because all of the work that I’ve done up to this point as been—for the most part—command line free.
To create a new application, just navigate to the directory in which you want your app to live and type:
C:\Rails_Directory> rails nameofapplication
At this point, the console window will start spewing out all kinds of text. Just let it go… it’s creating the directory structure for your new application. The root of this structure will be whatever you chose as your “nameofapplication.”
Starting the Server
Now that we have the basic structure of the application created for us, we need to start up our web server so we can view it through a browser. Again, it’s quite simple:
C:\Rails_Directory> cd nameofapplication
C:\Rails_Directory\nameofapplication> ruby script/server
More words will fill the box. Who knows what most of them mean, but they’re doing something. And that something is starting the web server. You can view what you have so far at http://localhost:3000! Of course, we’ll need to write some code before any of this becomes usable in anyway, but it’s just cool to see something working. One note: To the best of my knowledge, starting the server seems to render this particular console window useless—at least for more typing. Just open another one and we’ll continue.
Creating a database
So now we’re going to set up the database. Since there are three environments in Rails (development, test, and production,) we’re going to create three different databases for our application. First we have to change to a mysql command prompt.
C:\Rails_Directory\nameofapplication> mysql -u root
mysql> create database nameofapplication_development;
mysql> create database nameofapplication_test;
mysql> create database nameofapplication_production;
And that’s it. Now we’ve installed rails, created the framework for a program, started a server so we can view the program, and built the database. It’s pretty amazing how fast you can get set up in rails. Oh yeah, one last thing. To get back to a usable Rails command prompt, just type:
mysql> quit
Bye
Off the Charts!
There’s a gauge on the back of the “Agile Web Development with Rails” book I’m reading that is supposed to suggest the skill range for a potential reader. The range goes from just a wee bit above beginner all the way to expert. When I was shopping for this book, that caught my eye. “I’m at least a wee bit above beginner!” I told myself. Yeah… turns out that I was wrong. In the eyes of the Pragmatic Programmers (the book publishers) I’m an idiot of unspeakable levels—barely capable looking after myself. If I had been born in the 50’s, my parents would have taken one look at me and sent me “away,” forbidding my sisters to ever speak word of me again. I would have been the deep dark family secret. Okay, that’s a little overly dramatic, but I’m clearly sub-beginner in the eyes of those who publish this book, because the damn thing moves too fast for me.
This is the problem that I always have with programming books, classes, lectures, etc. They always start out really slowly, and then all of the sudden, BAM! I’m lost. This book isn’t quite that bad, but I’m definitely getting the feeling that the author is assuming that I know something that I don’t. And he’s rolling ahead without asking me if I’m following along. I don’t want to knock the book too much. I do think it will come in handy once I have a more basic understanding of what we are doing and WHY we are doing it. That’s the part that is missing here.
I was feeling a little frustrated about this (as you may have noticed in my previous post) so I searched online to see if anyone else has had this problem. It turns out I’m not alone. Many said that the Agile book was a little too advanced for the serious n00b and that a better option for someone like me was a book called Build Your Own Ruby on Rails Web Applications by Patrick Lenz. The book is published by Sitepoint, and they even offer the first four chapters for free (pdf) on their site. I’ve gotten through three of the chapters so far and I have to say this is much more my speed. It’s more conversational and Patrick explains everything very clearly. I ordered a copy last night on Amazon (way cheaper than buying it through Sitepoint) so now I just have to wait for it’s arrival. Yay. Hopefully this will be the answer I’m looking for. My desire to learn Ruby hasn’t dampened… I just need the right materials for the way my mind works.
Am I learning?
I’m not sure what to think about my current progress. The book I’m working with attempts to teach the reader Ruby on Rails by walking them through the steps to building a web application. Once you reach the end of the book, you’ll have built a working shopping cart (of course). I’ve just completed the first chapter of actual coding and I do have a nifty little admin page that allows me to add products to the database, display them, etc. The problem is that while I’m following the steps directly, I don’t feel like each step is being explained thoroughly. I often don’t understand why I’m doing something or why it’s working the way it does. Part of me thinks that I just need to get used to the syntax and the general structure of the program and then it will all make sense. But the other part of me worries that I’m not actually learning, that I’m just following instructions. I wish I knew somebody who was interested in learning this with me… that way we could bounce questions and ideas off each other.
Or maybe I need to stop worrying and just keep following what the book tells me to do. Sometimes you just need to do things a couple of times before it truly makes sense.
Ruby on Rails 101: Understanding MVC… or Not
For this portion of the learning experience, I will rely mostly on the book “Agile Web Development with Rails” (unless I don’t like it, in which case I’ll switch to something else). This book has been highly praised across the board as THE text to read if you want to learn Rails… and I want to learn Rails… so, there you go.
This week I’ve been studying the architecture that Rails is based on. It’s called MVC, which is an acronym for models, views, and controllers. I have to be honest and say that I’m not sure I understand it completely. I feel like I get about 80% of it, but there’s that last 20% that’s just not clear. My problem is mostly with how it relates to RoR and the directory structure Rails generates. I think that I really just need to code something to fully grasp how it works and what it all means. I’m more of a hands-on kind of guy, so I actually need to do it myself before I truly understand it—just reading it in a book doesn’t always cut it for me.
I’m tempted to skip all of this background info (about MVC and such) but I don’t think that would be wise. I’m eager to jump right in and start coding but RoR is a very rigid system, so if I don’t understand the framework, that’s going to be a major problem for me in the future. It won’t be that long before I can get my hands dirty.