DEV Community

davidka7
davidka7

Posted on

Default error in ruby

Before I was aware of the default errors that ruby could provide, I would always write my own custom error validations inside my controller. And so I just wanted to share with others the most basic syntax, for the right defaulted validation.
This is a code that temporarily saves your errors, only if the method "valid?" returns false. Here is the code "flash[:errors] = #{#any instance you have saved your create, update, edit method too or anything that is being validated}.errors.full_messages ". The error here runs a validation enumerable, and after you choose a style for way it saves the error, I chose the most original which was full_messages. Ruby is smart enough to realize what you are doing. This would automatically save the errors that are based off the validations that you have edited inside your active record methods. For Example if in the you methods you had, validates :column, presence: true, validates, uniqueness: true, then you could show its errors if there are any, by writing in its rendered value "flash[:errors]" in the presented page. If both are false, or either one, it would output a text like, "duplicate already exists", or "the name is empty". By using this default structure you can save a lot of time, and effort, and it's pretty convenient.

Top comments (0)