Archive for the ‘Tutorials’ Category

Testing in Rails: Part 1 - Unit Testing in Ruby

Wednesday, November 14th, 2007

This is part of an ongoing series of posts about how to get started writing tests for Ruby on Rails. The series begins with the introduction and overview of the ideas behind testing.

Unit Testing

Volt gauge

I am going to cover several types of tests over the course of this tutorial: unit tests, functional tests, integration tests. I will start with unit tests. Unit tests are a good way to introduce the subject of testing and, if you already have a working Rails application, they are the first tests you should consider adding. Think of unit tests as our first testing layer, a foundation for the other layers.

Unit testing is a testing technique to validate that individual “units” of code are working properly. A “unit” refers to the smallest testable part of the code. When unit testing, we are not interested in how all the units fit together, just in make sure that each unit is functioning properly on its own.

(more…)

Testing in Rails: Introduction

Saturday, November 10th, 2007

Meter face

For some time, I’ve been wanting to teach how to get started writing tests for Ruby on Rails. Eventually I’d like to do a video training on the subject, but until then I’ve decided to write a series of blog posts as a tutorial.

Everyone agrees that testing is important, yet a small percentage of developers actually write tests. That’s understandable because there is a lot to learn when first starting with Ruby on Rails: installation, Ruby, the MVC framework, Rails, ActiveRecord, migrations, validations, routes, pagination, plug-ins, deployment… There’s more than enough to keep beginners busy during their first few projects. Testing requires learning yet another skill for something that seems like it can be put off until later. After all, you just want to get the site built, right?

Experienced developers often preach to beginners how important it is to test. I’ve even heard it said: “You are not a professional developer unless you write tests.” That is simply not true. You can write great applications, have lots of clients and make lots of money without writing tests. There are thousands of professional developers who don’t. But what I think they really mean by that statement is: Once you start writing tests, you’ll reap a lot of benefits (saving time and writing better code), take your programming to another level, and have a competitive edge over developers who don’t write tests. And who doesn’t want to save time and write better code???

But how do you get there? If you are a Ruby on Rails beginner, focus on the essentials first. Write an application or two. You can start writing tests from the beginning, but I think it’s best if you learn to program in Ruby and have a solid understanding of what Rails is doing first. After you have the fundamentals down, you’ll be ready to learn to write tests. Don’t worry, it’s not as difficult as it might seem. I’m going to walk you through the process step-by-step. (It’ll be an ongoing project spread out over many blog posts.)

(more…)

Ruby on Rails Beyond the Basics

Thursday, October 18th, 2007

RoR BTB package

I’m happy to announce that my latest video training, Ruby on Rails Beyond the Basics, has been released on the lynda.com Online Training Library.

Ruby on Rails Beyond the Basics is the sequel to Ruby on Rails Essential Training and is 11.5 hours of video training for web developers who already feel comfortable with the fundamentals of Ruby and the Rails MVC framework. I teach more of the Ruby language and spend a lot of time on code blocks. Then, using the example of a blog as a project, I show you how to use migrations, demonstrate how to create CRUD that doesn’t simply mimic scaffold, and give you a better understanding of ActiveRecord and some of the pitfalls that often trip up developers. I also cover validations, callbacks, routes, pagination, performance improvements and more.

Ruby on Rails Beyond the Basics is available online as streaming video to subscribers of the lynda.com Online Training Library ($25/month, $250/year, $375/year with exercise files included). It is will also be available as a CD-ROM soon both through lynda.com and Amazon.com.

If you are a beginner, you will be better off if you first start with Ruby on Rails Essential Training which is available online to lynda.com OTL subscribers and on CD-ROM, both through lynda.com and Amazon.com, for $99.95.

If you try either of them and find that they are helpful, be sure to let me know!

Topics include:

  • Updating Ruby, Rails and projects
  • Using irb and the Rails console
  • Intermediate Ruby language techniques
  • Understanding code blocks
  • Catching errors and raising exceptions
  • Using migrations to manage database changes
  • Understanding ActiveRecord and avoiding common pitfalls
  • Creating flexible controllers and views
  • Improving performance with eager loading and cache counters
  • Working with form validations and model callbacks
  • Using partials and helpers to organize views
  • Salting passwords for increased security
  • Choosing how to store session data
  • Understanding and defining custom and named routes
  • Pagination
  • Using plug-ins

PHP with MySQL Essential Training on CD-ROM

Friday, August 24th, 2007

PHP with MySQL Essential Training

I already announced the online release of PHP with MySQL Essential Training. I’m really happy that it’s been getting tons positive user feedback. It seems to be helping a lot of people understand PHP as well as web development in general and that’s very gratifying.

Now you can also purchase the CD-ROM version. It’s $149.95 for 10.75 hours of training on 2 CDs. You can purchase it from lynda.com’s store or from Amazon.com.

The nice thing about the online version is that you can access it anytime, anywhere and you’ll also get access to all the other great lynda.com titles (including my Ruby on Rails Essential Training).

Why might the CD-ROM be right for you? A few reasons come to mind: No internet connection is required so you can watch offline or even when you travel. You’ll have speedier access to higher-quality video and sound. It includes the tutorial files so you can work along with me. You’ll have it on your shelf to refer to anytime, even if you don’t maintain your lynda.com subscription (but why wouldn’t you want do that?). You can share copies around your office or school. For large organizations, lynda.com can work out multiple-copy, site license deals too. And some people like having both the offline and online versions—one for the office, one for home or on the road.

If you’ve already watched either the PHP or Ruby on Rails training found it helpful, I hope you’ll consider taking a minute to write a review on Amazon.com with your opinion. Amazon.com shoppers don’t always know Lynda.com’s great reputation and your review will help them trust that the training can be helpful to them too.

The Monthly Bug

Wednesday, August 1st, 2007

Monthly Bug

I stumbled upon an odd Ruby on Rails bug this week. It was difficult to track down because it was a bug that only affected some users some of the time. Out of every 100 users: 50 would have no problems, 42 would have problems 2% of the time, 8 would have problems 8% of the time. Making it more difficult to pinpoint—during 92% of the year none of the users experienced any problems at all! Let me demonstrate the problem and explain how to solve it.

Let’s say we have a form for a user to fill out. On that form, we want the user to provide a date, but we don’t care about the day of the month; we just want the month and year. We might be asking for their birth month and year or how long they’ve been using our product. The most common example would be asking for a credit card expiration date.

(more…)

PHP with MySQL Essential Training

Tuesday, July 3rd, 2007

PHP with MySQL Essential Training

I’m happy to announce PHP with MySQL Essential Training has been released on the lynda.com Online Training Library. It’s almost 11 hours of video training on PHP. I start with the basics of the PHP language and work up to dynamic web pages with a MySQL backend by demonstrating how to build a content management system.

Some of the topics it covers include: installation, PHP language fundamentals (variables, arrays, loops, functions, etc.), blueprinting an application, building dynamic pages, structuring and interacting with databases, how to build, validate and process forms, and regulating user access with passwords. I also provide practical advice, give best practices examples, and demonstrate refactoring techniques to improve existing code.

PHP with MySQL Essential Training is available online as streaming video to subscribers of the lynda.com Online Training Library ($25/month, $250/year, $375/year with exercise files included). It is will also be available as a CD-ROM soon both through Lynda.com and Amazon.com.

I also recorded Ruby on Rails Essential Training for the lynda.com Online Training Library which is available online to lynda.com OTL subscribers and on CD-ROM, both through lynda.com and Amazon.com, for $99.95.

If you try any of them and find that they are helpful, be sure to let me know!

Ternary Operators

Tuesday, June 12th, 2007

Ternary Operator

You probably already know how to write if-then statements in PHP and Ruby.

<?php // a simple PHP example
  if (condition) {
    statement1;
  } else {
    statement2;
  }
?>

But have you discovered the usefulness of ternary operators yet?

(more…)

Rails Documentation

Tuesday, May 29th, 2007

Clip art licensed from the Clip Art Gallery on DiscoverySchool.com

Ruby on Rails documentation has been a source of frustration for many developers.

The official docs at api.rubyonrails.com are a great start. They are generated using RDoc. It’s a pretty slick system that extracts comments embedded in the Rails source code and turns them into HTML pages. Since the documentation exists with the source, it’s easy for the Rails Core team to review the docs while coding and to make quick updates when the source changes. And it’s just as easy for the HTML documentation to include the relevant bits of the actual source code for review.

But there are a few drawbacks in the official documentation too. I’ll review these problems, their solutions and how you can help to improve Rails after the jump. I’ll also show you how you can generate a portable, “offline version” of the documentation.

(more…)

Pagination in Rails

Thursday, May 24th, 2007

Pagination

Pagination is extremely important in any web application. Let’s say a client has a database with 10,000 cars for sale. You, the intrepid web developer, are hired to build a web front end to the database. You would never want to return a list of all 10,000 cars at once! When the web browser finished displaying the list (several days later), the user’s eyes would either glaze over from information overload or be left scrolling down the page for miles.

Instead you would opt for a more sensible approach and only return a subset of the list to the user—a “page”. The first page of results might have just 30 cars on it and include a link to the next page. That page would have another 30 cars listed and a link to both the next page and previous page. Or you might decide to show a list of all pages with links to allow the user to skip around. You would probably include a description on the page to communicate what part of the subset a user is viewing (e.g. “Page 1 of 20″ or “Cars 1-30 out of 10,000″). That is pagination.

Google provides a good example of pagination. Search for any word. Look at the top of the page and you’ll see the description of the page results “Results 1 - 10 of about 1,000,000 for…”. Look at the bottom of the page and you’ll see “1 2 3 4 … Next” underneath the expanding o’s in Google. This isn’t the only way to navigate paginated pages, but it illustrates the concept clearly.

Google Pagination

After the jump, I’ll demonstrate three different techniques for performing pagination on database records in Ruby on Rails.

(more…)

Time for strftime

Wednesday, May 16th, 2007

Big Ben clockface

The strftime function is a common way for programmers to convert a computer time into a human-readable string. You can pronounce it “string-f-time”, “string-from-time”, or “string-format-time”. While Ruby and PHP each have their own implementation, the function is actually part of the Unix open spec. That’s great news for developers because it means that we can reuse the formatting techniques in any language. I’ve also found that the Ruby/Rails docs are a little lacking and you might not have known that there were more options available. Since it’s not just for Ruby, we don’t have to rely on them as our sole source of information.

Keep reading and I’ll show you how to use strftime, give you a full list of format codes (downloadable even), and demonstrate a useful helper function for working around one of the quirks of strftime.

(more…)