Hands-on Rails: Come code along
Now that I've gotten a small taste of Ruby, it's on to the Rails development framework. To start a new project in Rails, you type
rails yourprojectname
in a Rails console.
If you'd like, follow along and create your own project. If you're using InstantRails on Windows, click on the I at the upper left and select Rails Applications --> Manage Rails Applications. That gives you the option of creating a new Rails app, which opens a shell in the correct rails_app
directory.
I type
rails myfavorites
This creates a bunch of new directories -- a dozen at the top level plus even more subdirectories.
Numerous directories and files are generated automatically when you first create a Rails project. |
If you're used to starting small with a couple of unstructured files, "this wealth of subdirectories can be overwhelming at first," Lenz says.
Yup.
But, he explains, "a lot of thought has gone into establishing and naming the folders, and the result is an application whose file system is well structured." This makes possible a lot of automatic associations between variables, objects, folders, databases and the like. The upside? You have a lot less work to do.
Architecture fundamentals
Rails follows the Model-View-Controller (MVC) architectural principles. Basically, Lenz explains, models handle data and business logic; controllers deal with application logic and information being passed to the user interface; and views handle GUI and presentation HTML that displays in a browser.
In Rails, the model, view and controller files are created in their own automatically generated subdirectories, one of the dozen-plus that live under the auto-generated main project directory, app/project
.
If you're used to merging everything together in a single file, as I am, it takes some getting used to. Initially, the concept seems downright silly: Create one file in my app/models
directory to set up the data structure; create another file in my app/controllers
directory to set up calculations, database queries and other logic; create yet a third file in my app/views
directory with the HTML code. All this just to accomplish the simple task of displaying something. Good grief, I can do all that with a few lines of code in a single PHP or Perl file and be done.
However, what looks like a lot of initial complexity and constraints is actually the foundation for less repetitive, more maintainable code. Follow the rules, and I'll know where each part of my application lives; I won't have to make changes by hunting for code scattered through a bazillion separate .php files.
As soon as a project is created, Rails sets up three different environments for it: development, test and production, expecting separate databases for each. "Configuring the database for a Rails application is frighteningly easy -- all of the critical information is contained in just one file," Lenz assures.
He's right, it's pretty slick. The file database.yml
is one of many that are generated automatically when I issue the rails myfavorites
command. Database.yml
lives in the config directory, another one created when you first start a Rails project. Since I called my project myfavorites
, my database configuration file contents look like this:
Hands on with Ruby on Rails
|
# MySQL (default setup). Versions 4.1 and 5.0 are recommended.
development:
adapter: mysql
database: myfavorites_development
username: root
password:
host: localhost
test:
adapter: mysql
database: myfavorites_test
username: root
password:
host: localhost
production:
adapter: mysql
database: myfavorites_production
username: root
password:
host: localhost
Alas, the databases themselves aren't created automatically when I issue the rails myfavorites
command, so my next step is to create them in MySQL.
In my InstantRails command-line console, I type in
mysql -u root
CREATE DATABASE myfavorites_development;
CREATE DATABASE myfavorites_test;
CREATE DATABASE myfavorites_production;
and then exit. (The exit part is important, otherwise you'll still be in MySQL when you're trying to execute Rails commands.)