$30 off During Our Annual Pro Sale. View Details »

[教育訓練] Rails Validation Basic

HeChien Hsu
September 04, 2012

[教育訓練] Rails Validation Basic

HeChien Hsu

September 04, 2012
Tweet

More Decks by HeChien Hsu

Other Decks in Technology

Transcript

  1. Rails Validation
    徐赫謙

    View Slide

  2. Rails Validation
    Basic

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide