A non-Jedi approach for new Rails coders:

Posted by yossarian

Note: this is part 2 in a series of articles for people who are involved in social movements and want to learn how to do web programming. The first installment is here. This article assumes that you already completed the other tutorial and are freaked out by the command line terminal.

Second in our “friendly web programming” series, this installment starts with a pop-quiz for new programmers. If you’re trying to learn programming for the first time, you’ve got a whole lot of unfamiliar stuff to think about, and most of the time you don’t understand at least half of what’s going on. In such a situation, which of the following two options would you like to be presented with when you are trying to learn web programming?

Choice 1

Simple. Elegant. Deadly. The weapon of choice for every real fire-breathing, capitalism-smashing leftist coder. Bonus points if you use the terminal to issue political communiques reading ...and if necessary we will compile our last lines of sourcecode on the barricades during a squat eviction. This has actually happened. The terminal is amazing powerful.

Or:

Choice 2

Looks like something you’ve seen before. Has a bunch of windows, buttons to click, allows you to get an overview of your project, and doesn’t require you to memorize forty or fifty cryptic commands before you can actually see something working.

Assuming you’ve picked choice 2, here’s how to get the software installed.

Download the Aptana Radrails integrated development environment (IDE). You can grab this at http://aptana.com/studio/download. Since you’re on Linux, pick the one that says Linux. Ignore all the stuff about the “Aptana professional” trial.

It’ll download as a .zip file. You should be able to extract it to a folder. Usually I make a spot in my home directory, i.e. /home/yossarian/software/aptana and stick the folder in there for safe-keeping.

Unfortunately, before you can run it you’re going to need to install a newer version of Java than you’ve probably got right now.

Enter this in a terminal:

sudo apt-get install sun-java5-jre

I’m assuming you’re on either Debian or Ubuntu GNU/Linux here. If you’re not, there’s probably some way to do it but you’ll have to figure that out for yourself.

During the installation you’ll be presented with a big scary screen like this:

You’ll need to hit the “enter” key, then accept the license (use the arrow keys to navigate and hit “enter” to select). The fear you are feeling is corporate in origin – Java is currently not Free Software, although the rumour is that the hive-brain of Sun Microsystems (who own the language) are planning to make it Free Software real soon now, so in theory you don’t need to feel too bad.

Ok, so you’ve got Aptana extracted into a folder, and you’re ready to see what it looks like. Since you didn’t install it with the regular package manager, it won’t show up in your programs menu automatically, and you will probably want a convenient way of starting it when you need to. Browse your way to wherever you put it, and there will be a “Aptana” file, on my Ubuntu system it looks like this:

In GNOME (the regular Ubuntu desktop), you can drag that “Aptana” file up to the bar at the top of the screen to make an application launcher so you can start it when you want. Enter “Aptana” as the application name, and if you’re feeling esthetically-conscious you can pick an icon for it in the dialogue on the left. Once this is done, click on the launcher to launch it (or just double-click the Aptana icon in the folder where you installed it, it makes no difference).

You’ll be presented with a screen like this one, just tick the “don’t bother me about this anymore” tickbox and accept the default location unless you have strong feelings about where your project files should go – you can click the “Browse…” button in the top right and select a different folder if you want to put your projects somewhere different.

At this point you should be looking at your shiny new integrated development environment. However, we’ve still got one round of Installing More Shit left before it’ll do you any good for Rails development. We’re getting close to the end of that, take my word for it.

Go to Help -> Software updates -> Find and install…

Select “Search for new features to install…” and hit the “next” button, and you’ll get a screen that looks like this:

You’re going to want to have the Firefox Browser Preview, the Ruby on Rails Development Environment and Subclipse.

Click Finish and you’ll be presented with yet another screen to agree to:

UPDATE: it may be the case that you can’t get past this step. If so, expand the Ruby on Rails Development Environment feature, and untick anything related to Mylyn.

Tick the boxes like in the graphic, say Next, agree to the license terms, and finally you can finish and start downloading. It’ll take a while to download the updates, then it’ll ask you if you really want to install – say “Install All”. Then it’ll ask you to restart Aptana. This may seem painful, but if you were updating something on Windows you would have already had to restart your whole computer four or five times to get this far. Trust me, I do it at work.

When it restarts, you’ll be presented with something that looks superficially the same as it did before you installed the Rails environment. This is merely a cunning deception. In theory, if everything went well, there should be some slight differences:

Note that in the top right corner of the screen, there’s now a red and white button that says “RadRails” on it. This means you’re now in Rails development mode. If this isn’t currently active, or you somehow manage to screw things up, you can go to Window -> Open Perspective -> Other… and select RadRails.

A pause to consider

So. Now you have a big complicated piece of software to write software with. An integrated development environment (IDE). Perhaps it makes you feel like now you have hit the big-time or maybe it just scares you. How does this monster stack up against the terminal, an elegant weapon from a more civilized time?

The answer is that the huge IDE you’ve just downloaded has a lot less functionality than the terminal does. For now, let’s look at this as a positive thing, because it cuts down on the range of choices available to you and provides some comforting, brightly-coloured buttons to push.

A new project

So, let’s make a new project.

Right-click on the big blank space on the left-hand side, and say New -> Rails Project. Call it foo, because that is the proper name to call anything when you’re programming and are faced with the sudden fact that you need to quickly think of a name (unless you’ve already used “foo”, in which case it is proper to call the next thing “bar”). Such are the arcane secrets of the programming world.

There will be a couple of tickboxes asking you whether you want to generate a Rails application skeleton (sounds creepy, but yes), and whether you want a WEBRick server (also yes). Hit the Finish button. At this point, nothing worked for me, because I’m writing this on a fresh computer and hadn’t installed Rails yet. If you haven’t yet installed Rails, follow the tutorial here.

Assuming it worked, you should be able to double-click on the “foo” folder – it’ll open up and you’ll see the contents of your new Rails project, just like in the earlier tutorial but a little more convenient since you don’t have to keep flipping back and forth between the terminal and your file browser to see things. It’s integrated. In theory you can do most of your work right in the IDE, although you’ll always have to dip into the terminal when things get crazy.

Once again, you’ll have to make a database (use MySql Administrator for this as in the earlier tutorial). Call it foo_development. Then you can switch back to Aptana and click on the Generators tab near the bottom of the screen. Select scaffold_resource from the drop-down menu on the left, and type

Article title:string body:text

into the text-entry field to the right of the drop-down. Then hit the Go button. A bunch of stuff should generate on your computer. To verify that this is the case, open up the app/controllers folder in the project browser on the top left side of the screen. There should be a file there called “articles_controller.rb”.

Note: The Aptana IDE is still pretty new and they are fixing lots of bugs. Sometimes for me, the Generators drop-down menu doesn’t show the generators properly. If this happens to you, just type in scaffold_resource where the drop-down is and follow the rest of the steps normally.

You still need to migrate your database – click on the Rake Tasks tab at the bottom of the screen, select the db:migrate task, and click the Go button on the right. You should get some stuff back in the console at the bottom of the Aptana screen saying that your migration ran ok, i.e.

== CreateArticles: migrated (0.0061s) =========================================

Note: sometimes the Rake Tasks drop-down screws up too. If it’s not showing any tasks for you, you can just type in db:migrate where the drop-down menu is.

At this point, you can start your development server by going to the Servers tab near the bottom of the screen. If you told Aptana to make a Rails server when you created the project “foo”, there should now be something called “fooServer” in the list of available servers. Click on it, and then click on the green arrow-shaped button which looks like the graphic designers copied it from a 1970s cassette tape player (iconography changes only slowly despite revolutionary changes in technological capability, it seems). This will start up the server.

At this point you should be able to open your browser and go to http://localhost:3000/articles. Before we go any further, let’s stop and consider what’s going on here.

Using the IDE, we just did exactly what we did using the terminal in the previous tutorial, except we did it pushing buttons. When we made a new project called foo, the IDE did exactly the same thing as we did in the terminal previously when we said

rails smashthestate

All of the other things we did through the IDE worked in a similar way – all the IDE did was issue the commands for us to our computer, instead of us typing them in. rake db:migrate was almost exactly the same as on the command line, and script/scaffold_resource might have looked vaguely familiar as well. Starting the server with that green cassette-player button (or stopping it with the red square “stop” button) is the same thing as doing script/server on the command line (or hitting ctrl-c to stop).

There is no reason to be optimistic

Don’t get totally comfortable with all of this comfort. Out on the scary web, or in books, far from the friendly escapegoat, nobody is going to tell you to “click the little green cassette-player button” – they’re going to say “start the server using script/server”. They won’t tell you to hit the “Go” button to run your database migrations, they’ll say to do “rake db:migrate” and expect that you know what they mean. However, quite a bit of it should be fairly intuitive once you get the hang of it.

At this point you’ve made two copies of the world’s ugliest blogging software. You may be thinking that all of this seems like a lot of effort for such a small result, but actually if you got this far (in either of the tutorials) you’ve accomplished quite a bit:

  • you’ve installed a computer language (Ruby)
  • you’ve installed a web development framework (Rails)
  • you’ve got an easy to work with integrated development environment (Aptana RadRails)
  • you’ve set up a database
  • you’ve used the code generation functions of Rails to generate a simple website structure.

The site you’ve got lacks any kind of layout or pleasant looking interface design, but it does allow you to save articles to the database, view them, edit them, and delete them. In the programming world this is considered pretty fast work. However, like anything else worth doing, getting to be a good programmer takes many hours of concentrated effort. Generating that Rails application skeleton, and running the database migration without really understanding what you’re doing, is only the first step on a long road.

Further reading

Over time, I’ll try to crank off a few beginner tutorials in Rails now that you’ve got the setup figured out. There are lots of other tutorials out there, though – even though it’s fairly old now, some of the best stuff is by Amy Hoy over at Slash7, if I was a beginning coder and wanted to learn, I’d start with her articles as they’re easy to read, informative, and don’t assume you’ve already got a degree in computer science. Start at the bottom of the list and work upwards, the oldest stuff is first.

There are a couple of tutorials by a fellow named Curt Hibbs that are also quite good. The Slash7 articles actually respond to these ones, trying to fill in some of the gaps for beginning coders.

The video tutorials at the Ruby on Rails site are pretty interesting and don’t take very long to do. The best single resource for learning Rails is a copy of the book Agile Web Development With Rails, Second Edition. It costs quite a few squids. If you can’t afford it, you can often find PDF copies of it floating around on the web,

It’s not as good as Q and offers no insight into how to spark a cultural revolution, but the Wu Ming can’t be on every case at all times.

Comments

Leave a response