Ruby on Rails is an open source web development stack with a large developer base. Last year, Ruby on Rails reached a critical milestone with the release of Ruby on Rails version 3.0. For more information about Ruby on Rails I recommend checking out www.rubyonrails.org. You can find installation information, documentation and links to other resources on this site. This article will demonstrate how to get up and running with Ruby on Rails with help from the RailsInstaller.
Ruby on Rails greatly simplifies the process of building web applications. Melding the power of the Ruby programming language with a well-constructed MVC (Model View Controller) architecture, Ruby on Rails represents a robust architecture for building web applications. This article will demonstrate some of the basic features of the Ruby on Rails development stack and give you a pathway to start exploring it.
The first step to using Ruby on Rails begins with installation. How you install Ruby on Rails depends greatly on your development platform. If your platform is Linux or OSX, a convenient way to install Ruby on Rails is to use the Ruby Version Manager (RVM). RVM is a shell script that helps you install different version of Ruby, different Ruby Gems (components) and entire Ruby on Rails configurations. You can find information for RVM, including installation and usage documentation, at https://rvm.beginrescueend.com/.
The RailsInstaller also provides a set of tools known as DevKit, which you use to compile Ruby Gems that contain native extensions, i.e., written in C or C++.
The last step of the RailsInstaller will ask if you want to configure Git and SSH. I recommend you answer yes to these questions. Developers commonly deploy their web Ruby on Rails applications using hosting companies such as Engine Yard or Heroku. Both of these companies use Git and SSH to deploy applications. Once you have completed all of the installation steps you will be presented with a command shell (Figure 2). When this command shell opens you can configure your Rails development environment.
Figure 2: The command shell lets you configure your Rails environment.
Once you are done with the RailsInstaller I recommend you install a decent text editor. For this article I will use Notepad++ with the Explorer plugin.
Now that you have installed Rails and your text editor you can build your first Rails application. To bootstrap your first Rails application, open the command shell provided by the RailsInstaller. You can access this from the RailsInstaller->Command Prompt with Ruby and Rails on your Start menu.
When you execute this command, Rails will create a new folder called codemag that will contain the directory structure for your Rails application. Figure 3 shows the output from the Rails new command.
Figure 3: There are two folders of note. The first is the app folder.
Building Your First Entity
Now that you have established your project you can go to work and build something useful. For this demonstration you will construct a set of Web pages for maintaining customer records using Rails built-in generator tools. One generator provided by Rails, known as the scaffold generator, will build all the files necessary to present and maintain an entity. To execute the generator, open the command shell and execute the following command:
When you run this command, Rails will generate a number of folders and files for this entity. Figure 4 shows the output of the scaffold generator.
Figure 4: Output from generating a new entity using the built-in Rails scaffold generator.
Notice that the scaffold generator creates a file in the db/migrations folder. This file, known as a database migration file, will be used to create the table for your entity. This is where you need to execute one more command, the rake command. One of the tools you will use in Ruby is rake.
Rake is used to automate a number of tasks including, but not limited to: running tests, running database migrations and running database seed operations.
Rake is used to automate a number of tasks including, but not limited to: running tests, running database migrations and running database seed operations. To create the table for the customer entity, execute the following command from the command line:
This completes all of the steps necessary to build your customer entity and all web pages for maintaining a list of customers. To test your applications you can now start the built test web server. To start the test web server, run the following command: