Slide 1

Slide 1 text

Rails Validation 徐赫謙

Slide 2

Slide 2 text

Rails Validation Basic

Slide 3

Slide 3 text

會經過驗證的 • create • create! • save • save! • update • update_attributes • update_attributes!

Slide 4

Slide 4 text

注意 • 有驚嘆號的(save!)驗證失敗會丟例外 • 沒驚嘆號的驗證失敗會回傳 • false: save, update_attributes • 物件: create, update

Slide 5

Slide 5 text

不會經過驗證的 decrement! • decrement_counter • increment! • increment_counter • toggle! • touch • update_all • update_attribute • update_column • update_counters

Slide 6

Slide 6 text

⼿手動跟驗證說掰掰 save({validate: false})

Slide 7

Slide 7 text

Demo https://gist.github.com/3508269#file_02_demo.rb

Slide 8

Slide 8 text

valid? invalid? https://gist.github.com/3508269#file_01_valid_invalid.rb

Slide 9

Slide 9 text

errors[] https://gist.github.com/3508269#file_01_valid_invalid.rb

Slide 10

Slide 10 text

helpers • acceptance • confirmation • exclusion • format • uniqueness • inclusion • length • numericality • presence • size • validates_associated

Slide 11

Slide 11 text

Example https://gist.github.com/3508269#file_04_demo.rb