Forms are crucial to how users interact with your site. You can slap a form on a web page in record time with Rails, but basic forms just get you by. For a better user experience, you’ll want to customize your forms and create interplay between your models. Learn how to take your forms to the next level from Ryan Bates, one of the most experienced Rails programmers (and screencasters) in the community.

About this Screencast

After watching these video tutorials, you’ll have moved beyond the basic forms used by the average Rails developer. You’ll not just know how to create better Rails forms, you’ll also understand why they work. That type of in-depth knowledge will enable you to create better forms in less time, and quickly fix any problems. On every Rails application you work on, you’ll be able to confidently apply the techniques the pros use to design full-featured forms with less code.

Whether you’ve been using Rails for a few days or a few years, you’ll likely learn something new in each of these episodes. In addition to the videos, you can also download the source code for each episode so you can experiment on your own.

All the source code is based on Rails 2.1.0 and 2.2.2.

Contents and Extracts

Throughout this series, we’ll build a pizza parlor application. You can choose from one of the house pizzas, or create your own custom pizza via a form. You can also search for pizzas that other people have created.

  • Episode 1: Form Essentials (30 minutes)

    We’ll start by building a search form, and then move on to a form for ordering custom pizzas. The pizza form starts out with a simple text field and checkbox, but ends up with a pull-down menu for the pizza crust type (a one-to-many association) and a group of checkboxes for the pizza toppings (a many-to-many association). You’ll learn how to:

    • use various form helpers to create form fields
    • create forms that are backed by Active Record models using form_for
    • create forms that aren’t backed by a model using form_tag
    • set default and initial values in form fields
    • use form field values in the params hash
    • prevent a common security problem related to mass-assignment
    • create pull-down menus for one-to-many associations
    • use checkboxes for many-to-many associations
  • Episode 2: Customizing Form Errors (25 minutes)

    Good forms help a user when trouble strikes. The default form error presentation is a start, but you should customize how form errors are presented to fit the needs of your users. Unfortunately, it’s not always obvious how to fully customize what Rails gives you. In this episode, we’ll completely customize the content of validation error messages and how form errors are displayed. We’ll finish up by validating the pizza form using AJAX. You’ll learn how to:

    • change the entire contents of any validation error message
    • customize the HTML (design and layout) of invalid form fields
    • make your application unique by writing a customized error_messages_for helper
    • display form errors inline next to invalid form fields
    • handle error messages added to “base”
    • validate forms before a submit using AJAX
  • Episode 3: Form Builders (29 minutes)

    Rails applications often contain many different forms, leading to lots of repetitive code. Reading and maintaining that code slows you down. In this episode, we’ll create a form builder to clean up our form code, and add some useful features along the way. Having all the form markup in one place means you can easily change the style of every form in your application. You’ll learn how to:

    • write a custom form builder
    • decorate standard form helpers with custom markup and style
    • handle field-specific options, such as required fields
    • present form field errors consistently in a form builder
    • use existing model validations to mark a field as being required
    • display inline error messages with a form builder

Audience

These screencasts are designed for beginner and intermediate Rails developers, but even the more experienced developers will get something out of the later episodes. Each episode focuses on forms, and assumes you have a basic understanding of the scaffold-generated RESTful controller actions, views, and models.

About the Author

Ryan Bates has been involved in web development since 1998. In 2005 he started working professionally with Ruby on Rails and is now best known for his work on Railscasts, the free Ruby on Rails screencast series.

Comments and Reviews

  • Peter Cooper said:

    The screencasts are of the high quality you’d expect from Ryan and are based on the latest Rails 2.2 standards. This alone makes them worth the price of entry from my point of view, as you get to see how to lay down forms in the most up to date way possible… I’ve been working with Rails since 2004 and I certainly picked up a few tips and tricks I wasn’t aware of!... They’re only $5 a pop and you’re going to save $5 of time by learning just one new trick from each.

    Rails Inside Review