If you have a basic grounding in the Ruby-on-Rails framework and are ready to dig in and build your first application, keep reading. This four-part article series will walk you through it. This article is excerpted from chapter three of the book Beginning Rails: From Novice to Professional, written by Jeffery Allan Hardy, Cloves Carneiro Jr. and Hampton Catlin (Apress; ISBN: 1590596862).
Building a Basic Ruby-on-Rails Application - Creating the Events Application (Page 2 of 2 )
As you saw in the previous chapter, the first step is to create a new Rails application. We could try to come up with a fancy Web 2.0 name, but instead we’ll keep it simple and just call the application events. It’s not going to win any awards for creativity, but it works.
To get started, issue therailscommand to generate the application skeleton and base files.
As you’ll recall from our little example in Chapter 2, therailscommand takes the name of the project you want to create as an argument and generates a directory of the same name that contains all the support files. In this case, it will create a subdirectory indevcalledevents. Change into theevents directory and get oriented. Figure 3-1 shows the directory structure.
Figure 3-1.The Rails directory structure
Our first stop is theconfig directory. Of the little configuration there is to do in a Rails application, most of it takes place in this aptly named location. To get an idea about what Rails is expecting as far as databases go, open thedatabase.ymlfile in your editor and take a peek. You should see something like the file shown in Listing 3-1 (we’re omitting the comments here).
Listing 3-1. The config/database.yml File
development: adapter: mysql database: events_development username: root password: host: localhost
test: adapter: mysql database: events_test username: root password: host: localhost
production: adapter: mysql database: events_production username: root password: host: localhost
The first thing you should notice is the different sections:development,test, andproduction. Rails understands the concept of environments and assumes you’ll be using a different database for each environment. Therefore, each has its own database connection settings, and different connection parameters will be used automatically. Rails applications run in development mode by default, so we really only need to worry about thedevelopment section here. Still, other than the database names (events_*), there should be little difference between the connection parameters for each environment.
Therailscommand has prefilled thedatabase parameter based on the project name:events. If you gave your application a different name (say, a snazzy Web 2.0 name, like,event.ilicio.us *beta), you would see something different here. In reality, it doesn’t matter what you name your databases, as long asdatabase.ymlreferences the correct one for each environment. We’ll stick with the convention and create the databases using the default names.
WHAT IS YAML?
The .yml extension refers to a YAML file. YAML is a special language for expressing objects in plain text. Rails can work with YAML natively and is able to turn what looks like plain text into Ruby objects that it can understand. It’s a recursive acronym that stands for YAML Ain’t Markup Language.
YAML is whitespace-active. It uses spaces (not tabs) to convey structure and meaning. Make sure your editor knows the difference between tabs and spaces, and make sure that when editing YAML files, you’re using only spaces. If you find yourself getting an error like /usr/lib/ruby/1.8/yaml.rb:119:in `load': parse error, you’re probably using tabs.
Please check back for the continuation of this article.
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.