Chapter 19. Ruby on Rails

image with no caption

Rails has become so closely connected with Ruby that it is now quite commonplace for people to talk about programming “in” Ruby on Rails as though “Ruby on Rails” were the name of the programming language.

In fact, Rails is a framework—a set of tools and code libraries—that can be used in cahoots with Ruby. It gives you the ability to develop database-driven websites that respond to user interaction. For example, users might enter and save data on one page and search for data on other pages. This makes Rails suitable for creating dynamic websites that generate web pages “on the fly” rather than loading up static, predesigned pages. Typical applications include collaborative sites such as online communities, multi-author books or wikis, shopping sites, discussion forums, and blogs.

I’ll provide a hands-on guide to creating a blog shortly. First, though, let’s take a closer look at the nitty-gritty details of the Rails framework.

Note

This chapter will give you a taste of developing in Ruby on Rails. Bear in mind, however, that Rails is a big and complex framework, and I will cover only the fundamental features. At the time of writing, Rails 3 is the latest version, but Rails 2 is still widely used; therefore, both versions are covered in this chapter.

Installing Rails

Rails is not a standard part of Ruby, so you may need to install it as a separate operation. Note that Ruby and Rails ship as standard with some operating systems, such as Mac OS X. There is, however, no guarantee that these are the latest versions, and you may want to update the default installation manually.

Do It Yourself . . .

There are various ways in which Rails can be installed. The easiest way is to use an all-in-one installer (some alternatives are described in this chapter). However, you may also install Rails and the tools it requires one at a time. Rails may be installed using the Ruby Gems “package manager.” As long as you are connected to the Internet, Ruby Gems will go online to find and install the latest version of Rails.

Note

For Ruby Gems documentation, go to http://docs.rubygems.org/.

At the command prompt, enter the following:

gem install rails

If, instead of installing the latest version, you want to install a specific version of Rails, you should append --version= followed by the appropriate version number when entering the previous command. For example, to install Rails 2.3.8, enter the following:

gem install rails --version=2.3.8

Alternatively, you can download and install Rails from the Ruby on Rails website, http://www.rubyonrails.org/. Most Rails applications require a database, which you will need to install as a separate operation. Many people use either SQLite or the free MySQL database server. MySQL is the more powerful of the two systems and is used on many professional websites. However, many people find SQLite simpler to use for local development of Ruby applications. Installation of SQLite varies according to which operating system is being used. SQLite3 is pre-installed on Mac OS X Leopard.

Installation of SQLite3 can be notably tricky on Windows. You should begin by running this command at the command line:

gem install sqlite3-ruby

Pay close attention to the message that is displayed when this executes. This tells you which version of the SQLite3 DLL you need to install and the web address from which you can download it. This DLL is a requirement. Failure to install it will mean that SQLite3 will not be available for use with Rails. This message will state something like this:

You've installed the binary version of sqlite3-ruby.
 It was built using SQLite3 version 3.7.3.
 It's recommended to use the exact same version to avoid potential issues.

 At the time of building this gem, the necessary DLL files where available
 in the following download:

 http://www.sqlite.org/sqlitedll-3_7_3.zip

 You can put the sqlite3.dll available in this package in your Ruby bin
 directory, for example C:Rubyin

Be sure to follow the instructions, download the correct DLL, and copy it into the in directory beneath your Ruby installation.

Refer to the SQLite site for more information on SQLite: http://www.sqlite.org/docs.html. You can find installation help on MySQL in Appendix B of this book. Many other database servers can also be used including Microsoft SQL Server, PostgresSQL, and Oracle.

Note

If you plan to install or update Rails from scratch or if you need to update the version pre-installed with your operating system, you should refer to Rails Guides website at http://guides.rubyonrails.org/getting_started.html. These guides provide detailed OS-specific information relating to Rails 3. Several Rails wikis also provide information on supporting older versions of Rails—for example, http://en.wikibooks.org/wiki/Ruby_on_Rails.

Or Use an “All-in-One” Installer

Various all-in-one Ruby and Rails setup programs are available. These include the Bitnami RubyStack installers for Windows, Linux, and Mac: http://www.bitnami.org/stack/rubystack/. Windows users can also use the Rails installer from RubyForge: http://www.rubyforge.org/frs/?group_id=167. These installers provide their own installation guides.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.218.226