Introduction

In July 2004 David Heinemeier Hansson released Ruby on Rails, a Web framework for the Ruby programming language. It quickly gained momentum and became notorious for being a highly productive Model-View-Controller (MVC) Web framework that was particularly well suited to Agile development.

Favoring sensible conventions over verbose configuration files, Rails (as it is often called for short) aimed to simplify and improve the lives of developers by allowing rapid prototyping of Web applications.

Fast-forward a few years, and Rails is now considered to be an established framework that has had a significant influence on the world of Web development. It inspired many other frameworks, helped boost the popularity of dynamic programming languages on the Web, and has been used to implement some of the largest sites that are out there today.

Despite Rails' wide success and adoption, its community still remains Unix-centric, favoring Mac OS X, GNU/Linux, or BSD variants over Microsoft Windows. Consequently the majority of books on the subject and most of the literature you'll find online assume that you are using Unix-like operating systems and tools. Though I personally use all of the operating systems I just mentioned, I believe that learning a new language and framework can be challenging in its own right, so there is no need to make the whole experience more difficult by adding a new operating system and its ecosystem of tools to the learning curve.

Until today, if you were to approach Rails as a "Microsoft developer" you would most likely be in for a culture shock. I felt compelled to change that by writing a book that helps bridge that cultural gap and make the experience of learning Rails, when you have a Microsoft background, much less traumatic. I attempt to be a friendly voice that can help you reach a better understanding of what Rails is and how you can use it for your own projects, while utilizing tools you are (already) comfortable with, leveraging your existing .NET or other Microsoft technologies skills, and understanding what caveats are associated with developing Ruby on Rails applications on Windows.

Who This Book Is For

This book is introductory in nature and as such is intended for beginner to intermediate programmers. Throughout the book I assume that you are somewhat familiar with object-oriented programming and Web application development, but not with Ruby or Rails in particular.

If you are an absolute beginner you may find some parts more challenging or obscure than others, but you should still be able to grasp the gist of things and get a glimpse of the bigger picture of what Rails can do for you. Conversely, if you are an experienced .NET developer, you'll probably appreciate a few in-depth considerations and the occasional comparison in the text, whenever similarities between the two worlds exist.

I also assume that you'll be using Windows, even though it is possible to follow along using any operating system, because the commands and examples are cross-platform.

To successfully keep up with this book, the following skills would be beneficial:

  • A general understanding of how the Web works

  • XHTML and JavaScript

  • A basic understanding of object-oriented programming (OOP)

  • Entry level knowledge of working with databases

Rails is extremely good at abstracting details. Therefore it provides you with one of the gentlest introductions to the world of Web development (if your current understanding of the Web is not overly advanced).

Several Ruby helpers and mechanisms are also provided by Rails, in order to minimize the amount of raw XHTML or JavaScript that you have to write. You don't have to be an expert in either of these languages, especially because this book is focused on Rails, not Ajax.

If you are in need of a refresher course for either XHTML or JavaScript, Appendix 1 provides links to excellent online material.

Ruby is important if you want to properly develop in Rails. Ruby is viewed as being multi-paradigm, but it's still fully object oriented. Understanding OOP will therefore help you become a better Rails programmer.

This book is general enough to be used by any developer, whether or not you're accustomed to Microsoft development models and tools. It is, however, particularly aimed at Microsoft developers. These days, when people talk about Microsoft programmers, they almost always inevitably think about .NET developers given that .NET has been the flagship development platform in the Microsoft world for several years. The book that you are holding in your hand now has been written with a clear understanding of this fact.

For example, in Chapter 2 you find a comparison overview of Rails and ASP.NET programming, whereas in Chapters 3 and 4, when I introduce you to the Ruby language, I refer to .NET languages (in particular C#) more than others.

The term "Microsoft developers" is rather broad and encompasses more than just .NET developers though. In this book I've (roughly) divided the spectrum of Microsoft developers into a few groups (not a complete list by any means). In your own specific case, different group descriptions could very easily overlap to describe the experiences, skills, and the roles that characterize your background.

Depending on which category (or categories, if more than one applies) you fall into, you'll have an easier or harder time meeting the preceding requirements and approaching the process of learning Ruby and RoR.

Microsoft Web Developers

I expect the majority of my readers to be Web developers. After all, this is a book about Web technologies; hence it wouldn't be surprising if the largest volume of interest came from Microsoft Web developers, who're planning on learning more about what all the fuss surrounding Rails is about.

If you're a Web developer, I have great news for you: the framework and the methods for creating applications may be different in Rails, but the Web is still the same. Your understanding of how the Web works, and of technologies like HTML, CSS, JavaScript, and Web Services, are a great starting point, which will help make you feel somewhat at home as you learn new Rails concepts.

If you are an ASP.NET developer, things get even better. Rails is much slimmer and simpler to learn than ASP.NET, and the fact that you have a solid understanding of object-oriented programming, through languages like C# and VB.NET, will ensure that you can quickly pick up Ruby, which happens to be much easier than the aforementioned languages, as well. Of course, the same can be said for C++ developers who may have old-school ISAPI programming experience.

ASP.NET developers will also benefit from being accustomed to a development environment where presentation and business logic are separated. The same thing can't always be said for ASP developers though, who also may not be as strong in terms of OOP skills, especially if VBScript is the sole language that they've worked with.

If you are an ASP classic developer, don't worry; essential Ruby knowledge is easy enough to grasp and, by following this book, you'll be able to learn Rails from scratch. In your case, your biggest asset is your knowledge of how the Web works.

Microsoft Windows Developers

Developers of MFC, Windows Forms, and WPF applications will find the MVC paradigm and the principle of separation of concerns rather intuitive, even when applied to the Web. And if you're well versed in programming languages like C++, C#, VB.NET, or Java, this will definitely play in your favor as you approach Ruby.

The biggest challenge for you will be switching to a relatively different platform. Programming in Rails is very different than producing desktop applications for Windows using Visual Studio.

On the bright side, if you've decided that it's time to give Web development a try, Rails will make your life much easier and will teach you a great new way of developing for the Web.

Microsoft Office Developers

If you are a Microsoft Office developer, you're in a somewhat similar scenario to that of a Windows developer (I'm assuming that you don't have any Web development experience yet). Your challenge is further increased, though, if you've only ever programmed in VBA and haven't switched to .NET yet. Again, in this book Ruby and Rails are taught from scratch, so you should be able to learn them both nevertheless.

Database Programmers

As a database programmer you have an invaluable asset when using Rails. Though ActiveRecord doesn't require advanced SQL skills from programmers, mastering SQL and database design will remove many limits from the set of possible applications that you can develop. Understanding the Active Record pattern will be much easier for you and you'll be able to truly take advantage of it.

Possessing a good knowledge of SQL will help you query the database, whenever the default abstractions provided by ActiveRecord are not enough for you or when a particularly efficient, custom query is needed. Conversely, when developing Rails projects you still decide what structure the database is going to have. A concrete understanding of normalization and its trade-offs, indexes, and advanced features such as stored procedures, will aid you when trying to decide if Rails conventions are sufficient, or when it's absolutely necessary to break the rules and do otherwise to satisfy your project's requirements.

Rails has an adapter for SQL Server, so if you decide to work with the database that you're most likely familiar with, you can do so.

Please note that, at this stage, there is little to no support for Microsoft Access databases. You can experiment with a rough draft of an adapter provided at http://blog.behindlogic.com/2007/07/msaccess-for-rails-heres-your-rough.html, but it's probably easier to just migrate your data to a different, fully supported database system.

How This Book Is Structured

The book begins by setting up the development environment and introducing the framework in the first two chapters.

Chapters 3 and 4 provide you with a crash course in Ruby programming.

In Chapters 5 and 6, a step-by-step sample blog application is developed. References to this simple app are occasionally made elsewhere in the book.

Chapters 7, 8 and 9 carefully examine the three components of the MVC triad, which is covered in the order of model, controller, and view.

Chapter 10 discusses exposing and consuming Web Services through Rails. And last but not least, Chapter 11 wraps up the book by covering the topic of moving into production, by briefly introducing security, performance and optimization, and deployment considerations. This last chapter also features a few pointers regarding aspects that may concern Enterprise developers.

Though the best results are probably achieved by reading the book cover to cover, this book was organized to be fairly modular and as such it's almost always possible to read chapters independently from the others.

That said, and as tempting as it is to jump right in, don't skip the first two chapters. They'll help you get started, set up your machine, and provide you with the right perspective to start building your knowledge of Rails. Likewise, unless you are familiar with Ruby, you shouldn't skip Chapters 3 and 4 either. Rails is a framework that is written in Ruby for Ruby programmers; having a solid understanding of Ruby will make your life a lot easier down the road.

What You Need to Use This Book

Throughout the book I assume that you're using Ruby 1.8.x and Rails 2.2.2. Newer versions will probably work too, with little to no adjustments, but it is recommended that you follow along using the same version as the book. The main idea is that once you've learned the main concepts, you will be able to use any future version of Rails. That said, when differences between Rails 2.2.2 and Rails 2.3 (the next Rails version, which has not been released yet at the time of writing) exist, these are pointed out. Other software that has been employed includes SQLite, a lightweight file-based database, and Mongrel, which is a relatively fast HTTP Web server. Instructions for installing this development stack on Windows, Mac OS X, and GNU/Linux are provided in Chapter 1.

Conventions

To help you get the most from the text and keep track of what's happening, we've used a number of conventions throughout the book.

NOTE

Boxes like this one hold important, not-to-be forgotten information that is directly relevant to the surrounding text.

Notes, tips, hints, tricks, and asides to the current discussion are offset and placed in italics like this.

As for styles in the text:

  • We highlight new terms and important words when we introduce them.

  • We show keyboard strokes like this: Ctrl+A.

  • We show file names, and code within the text like so: persistence.properties.

  • We present code in two different ways:

    We use a monofont type with no highlighting for most code examples.
    We use gray highlighting to emphasize code that's particularly important in the
    present context.

Source Code

As you work through the examples in this book, you may choose to either type in all the code manually or to use the source code files that accompany the book. All of the source code used in this book is available for download at http://www.wrox.com. Once at that site, simply locate the book's title (either by using the Search box or by using one of the title lists) and click the Download Code link on the book's detail page to obtain all the source code for the book.

Because many books have similar titles, you may find it easier to search by ISBN; this book's ISBN is 978-0-470-37495-5.

Once you've download the code, just decompress it with your favorite compression tool. Alternatively, you can go to the main Wrox code download page at http://www.wrox.com/dynamic/books/download.aspx to see the code available for this book and all other Wrox books.

Errata

We make every effort to ensure that there are no errors in the text or in the code. However, no one is perfect, and mistakes do occur. If you find an error in one of our books, like a spelling mistake or faulty piece of code, we would be very grateful for your feedback. By sending in errata you may save another reader hours of frustration and at the same time you will be helping us provide even higher quality information.

To find the errata page for this book, go to http://www.wrox.com and locate the title using the Search box or one of the title lists. Then, on the book details page, click the Book Errata link. On this page you can view all errata that has been submitted for this book and posted by Wrox editors. A complete book list including links to each book's errata is also available at www.wrox.com/misc-pages/booklist.shtml.

If you don't spot "your" error on the Book Errata page, go to www.wrox.com/contact/techsupport.shtml and complete the form there to send us the error you have found. We'll check the information and, if appropriate, post a message to the book's errata page and fix the problem in subsequent editions of the book.

p2p.wrox.com

For author and peer discussion, join the P2P forums at p2p.wrox.com. The forums are a Web-based system for you to post messages relating to Wrox books and related technologies and interact with other readers and technology users. The forums offer a subscription feature to e-mail you topics of interest of your choosing when new posts are made to the forums. Wrox authors, editors, other industry experts, and your fellow readers are present on these forums.

At http://p2p.wrox.com you will find a number of different forums that will help you not only as you read this book, but also as you develop your own applications. To join the forums, just follow these steps:

  1. Go to p2p.wrox.com and click the Register link.

  2. Read the terms of use and click Agree.

  3. Complete the required information to join as well as any optional information you wish to provide and click Submit.

  4. You will receive an e-mail with information describing how to verify your account and complete the joining process.

You can read messages in the forums without joining P2P but in order to post your own messages, you must join.

Once you join, you can post new messages and respond to messages other users post. You can read messages at any time on the Web. If you would like to have new messages from a particular forum e-mailed to you, click the Subscribe to this Forum icon by the forum name in the forum listing.

For more information about how to use the Wrox P2P, be sure to read the P2P FAQs for answers to questions about how the forum software works as well as many common questions specific to P2P and Wrox books. To read the FAQs, click the FAQ link on any P2P page.

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

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