Intro to Ruby on Rails

Posted By: Jas Singh

Ruby on Rails, often shortened to Rails or RoR, is an open source web application framework for the Ruby programming language that makes it easier to develop, deploy and maintain web apps. Rails uses the Model-View-Controller (MVC) architecture pattern to organize application programming.
It includes tools that make common development tasks easier “out of the box”, such as scaffolding that can automatically construct some of the models and views needed for a basic website. Also included are WEBrick, a simple ruby web server, and Rake, which is a build system.

Together with Rails these tools provide a basic development environment. One of Rails’ biggest strengths is metaprogramming, Rails introduces the Active Record framework, a design pattern by Martin Fowler. The rails version of Active Record discovers the columns in a database schema and automatically attaches them to your domain objects using metaprogramming. Rails also relies on convention over configuration. If you follow the suggested naming conventions, Rails does not need much configuration.  Rails also aides in creating start-up code using scaffolding so that you can get started on your code as soon as possible. These are few of the many features of Rails.

So let’s create a simple rails application! If you are using a Mac, Ruby on Rails is shipped with Mac OS X v10.5 and above. If you don’t have it installed it already, you can go to RubyonRails.org to download it for your target operating system. I am currently using Rails version 2.3.5. Also, I am going to be using NetBeans as my editor (it’s free!). Another very popular editor of choice if you are using a Mac is TextMate. I will be using the MySQL community server as my database and the MySQL Workbench tool, but you can easily use the database of your choice. SqLite 3 is the default database for Rails, starting with version 2.0.2. With SQLite 3 there are no steps required to create a database, and comes in handy if you need to get started quickly.


Open up the terminal window and navigate to a directory in your file system where you would like to create your application. For the purpose of this post, let’s say my working directory is /code/rails. Once you are in your working directory, use the rails command to create an application called demo.

rails --database=mysql demo



Once you have run the rails command, a directory named demo will be created with a bunch of files and subdirectories inside it. Open up the project using your favorite editor. Notice the directory structure rails has created for you. When I used the rails command, I used the --database switch, which tells rails which database to use, which is MySQL in our case. Now open up the project (under /code/rails/demo) and the directory structure should like this:

 



















Open up the database.yml file under the /demo/config directory.  Database.yml is the configuration file rails uses to connect to the database. Your configuration should look like:

database.yml contains 3 sections, one each for the development, test, and production databases. Each parameter has the following meaning:
adapter: the type of database
database: name of the database
username and password: credentials for the application to connect to the database
host: name of the machine hosting the database server.

Next, navigate to the demo directory and run the following command:

rake db:create

This will create the demo_development database. Next, run the migrate command:

rake db:migrate

This will create a schema_migrations table inside the database. Look at the figure below:



Schema_migrations is the table that supports Migrations in rails. A migration represents a change we want to make to the database, expressed in a source file in database-independent terms. These migrations can be applied to make updates to the database, and also rollback those changes. Next, we are going to use the generate command to create a new model. Rails automatically creates a migration that can be used to create the corresponding table. For the sake of this demo, let’s say we are trying to create a shopping cart. Users can view products in our inventory, and add the products they want to buy in the cart.

ruby script/generate scaffold product name:string description:text price:decimal

Next run the db:migrate command.

The above two commands create the product model, and also the products table inside our database. Note that we use “product” on the command line. In rails, a model (product) is automatically mapped to database table (products… plural form of the model’s class).  The rails generator created the model (/demo/app/controllers/products_controller.rb), views (edit.html.erb, index.html.erb,new.html.erb, show.html.erb inside /app/views directory), and the controller (products_controller.rb under /app/controllers).

Also, inside the db directory, rails creates the migration file (db/migrate/20100801185457_create_products.rb). The migration has a UTC-based timestamp prefix (20100801185457 in this demo, yours will be different), a name (create_products) and a file extension (.rb).


Next, you need to run this file to create the database. If you open this file, it looks something like this:

class CreateProducts < ActiveRecord::Migration
  def self.up
    create_table :products do |t|
      t.string :name
      t.text :description
      t.decimal :price

      t.timestamps
    end
  end

  def self.down
    drop_table :products
  end
end
Rails keeps track of the migrations which have been applied to the database. After you run the rake db:migrate command, go to the database, and you will see the products table created with the correct schema.

How does rails know which migrations have and have not been applied to the database? Take a look at the schema_migrations table after running the migration, and you will see an entry in the database with the value being 20100801185457, corresponding to the migration just ran.


At this point, you have an application with a database you can connect to. I am going to be using the WEBrick based web server supplied with rails. Run the following command:
ruby script/server
This starts a web server on the local host, port 3000. Next open up a browser window and type the url http://localhost:3000/products This .brings up the index view. How does rails know which controller action to call. To understand that, we need to look at rails routing. Open up the routes.rb file under the config directory. You will find the following line in there: map.resources :products That line is a way of defining RESTful resources. Map.resources will map the following actions in the corresponding controller: index, new, create, show, edit, update. Navigate the different pages of the application, and you should be able to create, view, update and delete a product. That’s all for creating a simple application to get started. In following posts, we will be looking at some more features rails has to offer.

Jas Singh is a Technical Lead for Fellowship Technologies. He has been with Fellowship Technologies since July 2007. He believes that every new day brings opportunities to learn something new.

Posted In: News, Tips,

Comments:
No one has commented yet. Be the first!
Commenting is not available in this channel entry.

Categories:

Previous Posts:


Subscribe to the RSS feed!