1 of 42

How to Validate

Data Input in Your

Rails App

Melissa Holmes

Rails Girls Atlanta - October 2014

2 of 42

in other words...

Melissa Holmes

Rails Girls Atlanta - October 2014

3 of 42

Let’s Validate...

Melissa Holmes

Rails Girls Atlanta - October 2014

4 of 42

Who am I?

5 of 42

Who am I?

Melissa Holmes

6 of 42

Who am I?

Melissa Holmes

Last year I switched from being a C++ programmer to working on web apps.

7 of 42

Who am I?

Melissa Holmes

Last year I switched from being a C++ programmer to working on web apps.

Currently I’m a Ruby on Rails & Sinatra developer at Rentpath.

8 of 42

Who am I?

Other interests:

9 of 42

Who am I?

Other interests:

Art/Drawing Gardening

Cats (all kinds) Knitting

Coffee & tea Reading

10 of 42

Validate?

11 of 42

Validate?

12 of 42

Our goal as developers:

13 of 42

Our goal as developers:

Don’t use or save invalid data.

14 of 42

Why?

15 of 42

The user must provide necessary and

properly formatted information needed to successfully complete an operation.

Why?

16 of 42

What?

17 of 42

Validate (a) required fields, (b) format correctness, and (c) confirmation fields.

What?

18 of 42

How?

19 of 42

Determine the expected format.

Apply appropriate rules in your code.

How?

20 of 42

Where?

21 of 42

Server-side: Model, Database

Client-side: Browser (View)

Where?

22 of 42

Server-side: ActiveRecord Model

Validations are written in the model’s .rb file

23 of 42

Server-side: ActiveRecord Model

Advantages

  1. Easy to maintain and understand
  2. Custom validation methods can be used
  3. Validation check built in for standard methods such as save and create
  4. Provides a second level of validation for critical data

24 of 42

Server-side: ActiveRecord Model

Disadvantages

  1. Active record does not enforce validation in all contexts or for all methods
  2. Passing validate: false to save skips validation
  3. Rails' uniqueness validator cannot guarantee uniqueness

25 of 42

Server-side: Database

Validations are written in migrations

26 of 42

Server-side: Database

Advantages

  1. Enforces uniqueness consistently and correctly
  2. Provides a second level of validation for critical data

27 of 42

Server-side: Database

Disadvantages

  1. Must access database (expensive and slow)
  2. May respond with an error that must be handled if the data is invalid
  3. Limited validation rules

28 of 42

Client-side: Browser (HTML)

Validations are written in view files

29 of 42

Client-side: Browser (HTML)

Advantages

  1. Uses native browser checks
  2. Fastest option

30 of 42

Client-side: Browser (HTML)

Disadvantages

  1. Can be bypassed by user
  2. Support varies by browser
  3. Look and feel varies by browser

31 of 42

Demo!

32 of 42

Demo!

33 of 42

Bonus Slides

34 of 42

Client-side: jQuery

Validations are written in javascript files

35 of 42

Client-side: jQuery

Advantages

  1. Reusable validation helpers
  2. Ability to do custom validation
  3. Less dependent on browser
  4. Faster than server-side validation

36 of 42

Client-side: jQuery

Disadvantages

  1. Javascript!
  2. Can be disabled by user
  3. Can be bypassed by user

37 of 42

Demo!

38 of 42

Demo!

39 of 42

Questions?

40 of 42

Server-side Sources

41 of 42

Client-side Sources

42 of 42

Contact me!

mel_tar

meltar

meltar