19,16 €
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.
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.
Learn to create dynamic websites with Ruby on Rails.
This book is a practical guide to creating a complete Web 2.0 website with Ruby on Rails. In this book, each chapter adds a new feature to the site, adding new knowledge, skills, and techniques. You will learn about the features of RoR by adding those features to your website. You will also learn about Software Development Life Cycle (SDLC) as each chapter closely follows the SDLC starting from requirement gathering and ending with testing.
This book is for anyone who has basic concepts of object-oriented programming as well as relational databases and wants to develop online applications using Ruby on Rails. Prior knowledge of Ruby or Rails is not expected.
Sie lesen das E-Book in den Legimi-Apps auf:
Seitenzahl: 225
Veröffentlichungsjahr: 2008
Copyright © 2008 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, Packt Publishing, nor its dealers or distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: March 2008
Production Reference: 1200308
Published by Packt Publishing Ltd.
32 Lincoln Road
Olton
Birmingham, B27 6PA, UK.
ISBN 978-1-847193-41-4
www.packtpub.com
Cover Image by Vinayak Chittar (<[email protected]>)
Author
A.P. Rajshekhar
Reviewer
Walt Stoneburner
Senior Acquisition Editor
Douglas Paterson
Development Editor
Ved Prakash Jha
Technical Editor
Mithun Sehgal
Editorial Team Leader
Mithil Kulkarni
Project Manager
Abhijeet Deobhakta
Project Coordinator
Zenab Kapasi
Indexers
Hemangini Bari
Monica Ajmera
Proofreader
Angie Butcher
Production Coordinator
Shantanu Zagade
Cover Work
Shantanu Zagade
A.P. Rajshekhar, Senior Developer with Vectorform, has worked on enterprise-level web applications and game development. His endeavors include the development of a Learning Management System, Supply Management Solution, and Xbox-based games. He holds a Masters Degree in Computer Applications. He is a regular contributor to the Devshed Portal on topics ranging from server-side development (JEE/.Net/RoR) to mobile (Symbian-based development) and game development (SDL and OpenGL) with a total readership of more than 1.4 million.
Authoring a book is not an easy feat. However, the help and guidance from my family and friends helped me to author this book. First, I would like to thank Packt Publication for providing me an opportunity to work on such an exciting project. I would like to thank my parents for their constant encouragement. Special thanks to my friends Shrikant Khare and Sormita Chakraborty for their support and encouragement.
Walt Stoneburner is a software architect with over 20 years of commercial application development and consulting experience. Fringe passions involve quality assurance, configuration management, and security. If cornered, he may actually admit to liking statistics and authoring documentation as well.
He's easily amused by programming language design, collaborative applications, and ASCII art. Self-described as a closet geek, Walt also evaluates software products and consumer electronics, draws cartoons, produces photography, writes humor pieces, performs slight of hand, enjoys game design, and can occasionally be found on ham radio.
Walt may be reached directly via email at <[email protected]>. He publishes a tech and humor blog called the Walt-O-Matic at http://www.wwco.com/~wls/blog/. Rumors suggest that some of his strange videography may be found on iTunes.
Currently he is employed at Business & Engineering Systems Corporation as a lead engineer developing advanced software solutions for knowledge management.
Other book reviews and contributions include AntiPatterns and Patterns in Software Configuration Management (ISBN 0-471-32929-0, p. xi) and Exploiting Software: How to Break Code (ISBN 0-201-78695-8, p. xxxiii).
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. It has been developed on the Ruby platform.
This book is a tutorial for creating a complete website with Ruby on Rails (RoR). 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.
Chapter 1 gives you an overview of the features of Ruby and RoR, as well as providing the various ways of installing, configuring, and testing both Ruby and RoR.
Chapter 2 introduces you to the basics of Ruby as well as the main concepts and components of RoR.
Chapter 3 makes you understand the design of tables according to the conventions of RoR, creation of scaffolds for tables, and changing the scaffolds according to the requirements.
Chapter 4 gives you details about how to set up the User Management module for the website called TaleWiki.
Chapter 5 makes you familiar with the Login Management and Comment Management modules for TaleWiki.
Chapter 6 introduces you to the Migrations and Layouts involved in setting up the template for TaleWiki.
Chapter 7 describes the tagging functionality being implemented for the enhanced search usability.
Chapter 8 provides you with the implementation of AJAX for TaleWiki.
Chapter 9 deals with the development of an interface for the administration.
Chapter 10 gives you the steps for deploying the website.
In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.
There are three styles for code. Code words in text are shown as follows: "For example, to add instance attributes named author and genre to the Tale class, you will do it as follows:"
A block of code will be set as follows:
When we wish to draw your attention to a particular part of a code block, the relevant lines or items will be made bold:
Any command-line input and output is written as follows:
New terms and important words are introduced in a bold-type font. Words that you see on the screen, in menus or dialog boxes for example, appear in our text like this: "In the next page, without entering any data, click on the Create button."
Important notes appear in a box like this.
Tips and tricks appear like this.
Feedback from our readers is always welcome. Let us know what you think about this book, what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.
To send us general feedback, simply drop an email to <[email protected]>, making sure to mention the book title in the subject of your message.
If there is a book that you need and would like to see us publish, please send us a note in the SUGGEST A TITLE form on www.packtpub.com or email <[email protected]>.
If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.
Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
Visit http://www.packtpub.com/files/code/3414_Code.zip to directly download the example code.
The downloadable files contain instructions on how to use them.
Although we have taken every care to ensure the accuracy of our contents, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in text or code—we would be grateful if you would report this to us. By doing this you can save other readers from frustration, and help to improve subsequent versions of this book. If you find any errata, report them by visiting http://www.packtpub.com/support, selecting your book, clicking on the Submit Errata link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata added to the list of existing errata. The existing errata can be viewed by selecting your title from http://www.packtpub.com/support.
You can contact us at <[email protected]> if you are having a problem with some aspect of the book, and we will do our best to address it.
'Which is the best framework for web application development?' This question is asked in different ways and forms. The answer, however, always remains the same—'The one that enhances productivity'. The next obvious query would be 'In that case which is the framework that enhances productivity?' After some debates and deliberations, we can conclude, 'A framework that reduces Boilerplate code and also reduces the learning curve is the one that increases productivity.'
If you look around, there are an abundance of frameworks that cater to web application development. But most of them fail in one of the two points that govern productivity. Either the framework reduces the Boilerplate code or it is easier to learn. Achieving a balance between the two is seen as a tough task. It is here that Ruby and Ruby on Rails (or RoR as it is fondly called), score above most of their contemporaries. How they achieve this balance is what we will be looking at in this book.
We will be developing a website throughout the book, each chapter adding something new to the website. This chapter will lay the groundwork of introducing you to Ruby and RoR. It will also tell you the ways to install and configure Ruby and RoR—one-click as well as manual installation, and finishing with techniques to test your installation.
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.
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:
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.
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.
The philosophy of RoR depends on two principles:
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:
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.
RoR can be installed in two ways:
If you already have Apache and MySQL installed, then manual installation is the better way as it installs only RoR.
There are three main steps for manually installing RoR, which are:
In this case, the RoR installation is done over the internet. So from the second step itself, ensure that the internet is connected throughout the installation.
First, grab the Ruby installer for windows at http://rubyinstaller.rubyforge.org/wiki/wiki.pl.
The previous figure shows the main page for the one-click Ruby installer (do not confuse it with one-click RoR installer). It provides a list of links that provide details about the one-click installer. Here are the steps for downloading and installing Ruby:
The next screen will present you with the components to be installed.
If the installation completes without any problem, then you will see the following screen:
That completes the Ruby installation. The next step is to update the installation using gem.
Gem is the name of the utility supplied with Ruby in order to manage, install, and update the Ruby installation in an easy way. The second step in the manual installation of RoR is updating the Ruby installation so that if a new package or an update for any of the package is available, then the complete installation can be made up-to-date.
Before we begin, if you are behind a proxy, open the command prompt and give the following command:
For example, if the address of the proxy server is 192.168.1.1 and the port number is 9090, then you would have to give the following at the command prompt:
Next, give the following command at the prompt:
You will get the following reply after the last step:
Select the compatible version for your platform. Here I choose option 2 which is the latest for Windows. The difference between <ruby> and <mswin32> is that the former is a pure Ruby-based package and the latter is the packages natively compiled for Windows. If there is more than one package to be updated, then more 'choice menus' will be presented to you. The point to remember is to choose the number corresponding to the latest version of the package natively compiled for Windows.
That completes the update gem step. Next, let us install RoR.
This is the last and the easiest part of the installation process. Just one command and RoR shall be installed. At the prompt, issue the following command:
The command should give the messages as shown in the previous figure. Congrats! RoR is now installed on your system.
For those working on GNU/Linux, only the first step would differ. To install Ruby, grab the latest tar file from http://www.ruby-lang.org/en/. Then give the following command at the prompt:
Then go into the directory created by the tar command. Inside the directory issue following commands:
That's it. Ruby is ready to be explored.
The one-click installer is, in fact, a zip package containing everything that you need. All you need to do is download it and unzip it to a directory of your choice. Even though, it is in a single package, the installation needs to be configured. Therefore, in essence, there are two steps:
http://instantrails.rubyforge.org/wiki/wiki.pl.
From the links listed on the page click on the [Download] link.On the next page, select the package corresponding to the latest release from the list.