Chapter 12 - Avatars

Introduction

Now that we've added a community listing, searching, and browsing, RailsSpace users can find each other, but the profiles they find are rather plain. In this chapter, we take the first step toward improving this situation by adding an avatar image upload so that users have a visual way to represent themselves on their profiles. Future enhancements include a friends listing (Chapter 14) and a blog (Chapters 15 and 16).

In the context of computers, an avatar is an image or other graphical representation of a particular person.1 On RailsSpace, the avatar will simply be an uploaded image. It can be a picture of the user, but it doesn't have to be; any image that expresses the user's personality will do. We'll put the avatar on the user hub and profile, and later we'll use a thumbnail version on the friends list (Chapter 14) and on blog comments (Chapter 16). Accomplishing all this will require understanding image uploads in Rails, as well as learning how to use Ruby to communicate with the underlying filesystem.

Table of Contents

  • 12.1 Preparing for avatar upload 365
    • 12.1.1 Adapting a model 366
    • 12.1.2 Avatar upload page 368
    • 12.1.3 An avatar partial 371
  • 12.2 Manipulating avatars 373
    • 12.2.1 ImageMagick and convert 374
    • 12.2.2 The save method 377
    • 12.2.3 Adding validations 378
    • 12.2.4 Deleting avatars 382
    • 12.2.5 Testing Avatars 385

Source Code

Notes / Tips

Errata

As of the first printing, these are the known corrections:

  1. p. 370. Listing 12.6 should be titled app/controllers/profile_controller.rb
  2. p. 376. In the footnote,
    ln -s /usr/local/bin/convert/usr/bin
    is missing a space; it should be
    ln -s /usr/local/bin/convert /usr/bin
  3. p. 386. Listing 12.21 should be titled test/test_helper.rb