Ruby On Rails – A Beginners Journey – Part 1
Let’s document my trip, shall we?
I set myself a goal at the beginning of the year to learn Ruby by the end of it. Well, I finally started doing just that. Actually I’ve tried a couple of times, but didn’t get far into it. By starting with Rails this time, I am actually picking up things a lot quicker. I’ll give all the credit to Michael Hartl, who wrote the book Ruby On Rails 3 Tutorial, which is what I am using, and so far recommend. You can also visit the site here. This isn’t a book review, though I might do later on. This post is the beginning of many that I’m going to use to document my progress. You know, show you where I was being stupid, and the awesome “ah-HA!” moments.
The beginning is the hardest
I’m talking about setup here, not writing any code. There are a lot of things that I had to do before I could start typing anything that looked like Ruby. First came Rails. Installing Rails is actually the easiest part of it. I didn’t have any trouble there.
Then came Git. Had to download an install that, and there is a bunch of stuff you need to do to get that to work with github.com. Be it the key you have to type in, create a repository, then do a test app and add the files, followed by commiting them, then pushing them…went on and on it seemed, but I got Git up and running. Go me!
Next came Heroku. A place to store your newly created app and view it online. Where Git is the version control, heroku.com is like the host. But in order for that to function right, you have to create an app space. Oh ya, install the Heroku gem by the way. Once you have the app space created, you then have to hook it up with Git, so you can use Git to push your app to Heroku. Luckily there is pretty good documentation for this stuff, so if anything goes wrong, which it will, you aren’t dead in the water.
Of course that doesn’t exactly mean everything will go smoothly.
Rails’ Gemfile is cool
This is a file that Rails creates where you tell it what gems your app needs in order to work. I have had a little experience with gems before because of my use of Sass, and recently Haml. While going through this glorious book it tells you what gems to require, and how to add them. So simple in fact that I decided let’s add a few just to see if it really IS that easy. In went Sass, Haml, and Compass. Then of course, you must bundle all this crap together with the “bundle install” at the command line. Piece of cake!
That is until I tried getting everything in the tutorial to work with Heroku. You see, in the book it tells you to include this line:
gem 'sqlite3-ruby', 1.2.5', :require => 'sqlite3'
This gave me an error when I tried to push it to Heroku. Why? Easy! Heroku doesn’t support SQLite databases. FREAKIN’ awesome. I then spent a long time figuring this crap out until a friend sent me to Stack Overflow for the fix. The funny part was that I had already seen that article and I swear I tried the solution. Apparently not though, because putting this in my Gemfile instead, worked:
group :production, :staging do gem "pg" end group :development, :test do gem "sqlite3-ruby", "~> 1.3.0", :require => "sqlite3" end
Everything then was handy dandy for a while. Until I added the Blueprint framework CSS and tried to push it to Heroku. Some error about the SCSS file not precompiled. Again I had to go and search Google’s pages to find the answer, which was to change a line to this:
config.assets.compile = true
I’m told testing is good!
Test-driven development is good according to this book. From what I know now, that is probably a good thing to get into the habit of. This led to installing rspec, which places files in Rails and lets your write test to see if things are working properly. Is the correct title showing up on the right page? Good. No? Fix it. Well, installing rspec was fine, but running my first test popped up an error about something or other. All I remember is having to install something called ANSICON v1.40. Can’t remember what it does, but that it was being asked for.
From rspec install, to first test, to installing ANSICON, to test again was probably about 40 minutes of time. Not terrible, but when you start adding all this stuff up and realize I haven’t actually done anything yet, you start to think that this isn’t as simple as you were hoping. Ruby might be an easy language to read, but getting all this crap going is a serious pain in the ass.
Actually learning something…priceless
At work I sometimes have to dive into the folder structure of Symfony, which is a framework for PHP. Never liked it. Finding crap was a pain because I could never remember where certain things were placed. Learning Rails though has cleared some things up a little. I now understand MVC a bit more. What the hell is MVC? Good question.
It stands for Model View Controller. The Model is what talks to the database. The Controller tells the View what to display and gives/takes things from the View to relate back to the Model. They really need to come out with a book for noobs explaining all the vocabulary in plain terms. Rails provides folders actually called: models, views, and controllers, so you actually get this stuff buried in your brain some.
I see the power of Haml more clearly
Within the folder structure you have a file that makes me think of “template”, called ‘application.html.erb.’ Which now that I just looked it up, apparently IS a templating system, just not as cool as Haml. You can go read my four part series on Haml at your leisure. One thing that really became apparent though is how perfect a fit Haml is with Rails. Rails seems to be about building things quickly and easy to read (due to Ruby), and Haml just adds to that. Now add in Sass? Holy F*$# this is getting exciting. I wish I didn’t feel so late to the party.
A good but difficult start
Well now, it looks like it is going to be a long trip. So far I feel like I have learned a lot, while at the same time having a voice in the back of my head telling me I’m not grasping all of it. Maybe it will take me a couple of passes through this book, who knows. What I do know is that I am having fun learning this, and that has kind of been missing for a little while. I need something to dig into, and Rails/Ruby seem to finally be it for me.
This is not a subject that the average front end developer is going to like unless you have been looking to get into a programming language. Most I know, have no desire to actually do that. A major barrier with Rails though, at least for the inexperienced, is the amount of time you spend using the command line and fixing problems just to get everything running smooth. So much depends on what version of whatever it is you are using, which can cause so many problems. If you add up all the time I spent just trying to get to the “Ok, NOW I can start writing crap” moment, most people would have given their computer the finger and said screw it. This is even more infuriating due to my book saying stupid crap like “If that doesn’t work, try using version 1.2.5. Beyond that use Google.” Ok, not that exactly, but really freaking close.
Anyway, I will write about my progress as I go along and learn or come upon. This is basically what I started this blog for almost two years ago. To show what I’ve learned and what bugged or created difficulty for me.