Book Image

Building Dynamic Web 2.0 Websites with Ruby on Rails

By : A P Rajshekhar
Book Image

Building Dynamic Web 2.0 Websites with Ruby on Rails

By: A P Rajshekhar

Overview of this book

<p>Ruby on Rails is an open-source web application framework ideally suited to building business applications; accelerating and simplifying the creation of database-driven websites. Often shortened to Rails or RoR, it provides a stack of tools to rapidly build web applications based on the Model-View-Controller design pattern.<br />&nbsp;<br />This book is a tutorial for creating a complete website with Ruby on Rails. It will teach you to develop database-backed web applications according to the Model-View-Controller pattern. It will take you on a joy ride right from installation to a complete dynamic website. All the applications discussed in this book will help you add exciting features to your website. This book will show you how to assemble RoR's features and leverage its power to design, develop, and deploy a fully featured website. Each chapter adds a new feature to the site, adding new knowledge, skills, and techniques.</p>
Table of Contents (16 chapters)
Building Dynamic Web 2.0 Websites with Ruby on Rails
Credits
About the Author
About the Reviewer
Preface
Index

Ruby and RoR—The Next Level in Dynamic Web Development


It is always a good idea to know about the specifications of the tool with which one has to work, before handling the tool. In our context, the tools are Ruby and RoR—Ruby as the language and RoR as the framework built upon Ruby.

Ruby

In 1995, Yukihiro Matsumoto released the first version of Ruby and this added one more language to the ever-growing toolkit of application developers. The current stable version is 1.8.6. According to the TIOBE Programming Community Index, it is the fastest growing language. So, what makes it the fastest growing one among the languages? To understand this, let us first understand the reason behind the creation of Ruby. The main reason given by Mr. Matsumoto for creating Ruby was that he wanted a scripting language that would optimize the way a programmer would develop the software. This means that the features of Ruby are such that they optimize the way the software is developed. What are these features? Let us have a look at them:

  • Interpreted: Ruby is an interpreted language. Therefore, whenever you make a change to the source code, you need not compile the code and then run it to see the effect of the change. As a result of this feature, the code-compile-run cycle becomes the code-run cycle.

  • Purely Object-Oriented: Ruby is purely object-oriented. That means that everything in Ruby is an object which includes primitive data-types and numbers. In addition, it supports all the features required by an Object-Oriented Language.

  • Functional: Ruby supports functional programming using blocks.

  • Duck Typing: It is also known as Dynamic Typing. Ruby decides about the type of variable while the program is running by looking at the value contained in the variable at that instant. In other words, if an object looks like a duck, sounds like a duck, then it is a duck!

  • Automatic Memory Management: You would know it as Garbage Collection. As in any Very High-Level Language (VHLL), Ruby provides Garbage Collection out-of-the-box, thus you need not worry about physical memory leaks.

  • Threading: The current stable version of Ruby provides 'almost' platform independent threading using green threads (threads used at the user-space level are known as green threads.) I said 'almost' because Ruby threads are simulated in the VM rather than running as native OS threads.

  • Reflection: Ruby provides a program with the ability to 'look at itself' while running. This ability is known by different terms, such as reflection, introspection, and so on. Using reflection, a program can modify certain aspects of itself during execution, or create a completely new object at runtime based on the requirements at that time.

Looking at the features we just discussed, you could definitely see that the creator's reason holds true. The way imperative programming features have been balanced with functional programming is the proof of that. It is on such a foundation that RoR has been built.

Ruby on Rails (RoR)

RoR is a recent entrant in the world of web application frameworks. So how come such a new player on the block not only stands on its own but can also challenge veteran players of the likes of J2EE/JEE? The answer does not just lie in the functionalities. The other aspect that governs the acceptance of a framework is its philosophy. Hence, we will have to look at both the aspects of RoR—functionality as well as philosophy. Keep in mind that the philosophy holds true for all the versions of RoR.

Philosophy

The philosophy of RoR depends on two principles:

  • DRY: Don't Repeat Yourself or the DRY principle, if applied properly, reduces the duplication of tasks within a project. Duplication of any kind, within a project, leads to difficulty in modification and maintenance and inconsistency. In RoR, you can see this principle at work in almost everything—from the reusable components in the form of plug-ins to the way database tables are mapped.

  • Convention-over-Configuration(CoC): Configuration has taken over the web application frameworks so much that even a simple task such as applying 'compulsory field' validation for just one field requires entries in an XML file. In RoR, the principle is to supply information about only those aspects that are different from usual application settings. The ORM (Object Relational Mapping) framework provided by RoR is an example of the Convention-over-Configuration principle. Unless you specify a different name for an ORM object, the object uses the name of the table to which it is mapped. Whereas in the case of configuration-based ORM frameworks, such as Hibernate, the mapping of each table along with its columns has to be given in the configuration file. So, a change in the schema means a change in the configuration file. However, in the case of RoR, a change in the schema doesn't mean a change in the object unless the name of the table itself changes. We will see more about the ORM framework in Chapter 2.

Features

The features based on the philosophy of DRY and Convention-over-Configuration principles are what make RoR so attractive for the development of dynamic web-sites. The features that showed the way for alternative methods for implementation of various server-side techniques are:

  • Automatic setup of Application structure: If you have worked with J2EE technologies, this would come as a pleasant surprise. The structure of any application need not be created manually. Just one command and the complete structure including folders and basic web files such as index.html will be generated for you. Therefore, no more hunting for third party tools such as those that provide the initial setup or setting up the structure manually.

  • Generation of Boilerplate Code: Every application has certain blocks of code that are essentially the same for all other applications of the same type or category. Such blocks are called Boilerplate code. One of the examples of Boilerplate code is the code block setting up a connection to the database. The same code can be used with different applications with only a little change. Though this is the case, most of the frameworks do not provide any inbuilt mechanism to reduce this 're-invention of the wheel'. RoR avoids the duplication using scaffolding. In essence, a scaffold contains the bare minimum of code to accomplish tasks such as connecting to the database, setting up a log, and so on. Scaffolds reflect the DRY principle that RoR adheres to.

  • Dynamic mapping of classes to database schemas: No web application can go online without having a database as its back-end. ORM frameworks have eased the database access. However, the configuration aspect reduces any advantage to the developer. In the case of RoR, ORM does not need any configuration. At runtime, RoR reads and maps the schema based on the names of classes and corresponding tables using reflection and meta-programming. Moreover, what the developer gets is more productivity.

  • Ajax at the core of presentation: Ajax is the technology that provides interactivity to websites without becoming intrusive. All the current server-side technologies claim to support Ajax, but the support is peripheral and not at the core. You would have to download new libraries, configure them, and then start the develop-compile-deploy-test cycle again. Whereas in RoR, Ajax is part of the core libraries. So when you install RoR, Ajax support is also made available to you. Using them is as easy as when you use any other library provided by RoR.

  • Batteries included: RoR contains many more libraries that provide for essentially all the requirements of a dynamic website including layout management, mailing, and so on. If you look at these libraries, you will understand that they are, in fact, fully-fledged components in themselves, representing different services provided by a website or a portal.

That completes the roundup of features of the 'tools' that we are going to use to build our website. The next step is to install and configure our 'tools' so that we can get started with our task.