DEV Community

davidka7
davidka7

Posted on

2 2

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.

Sentry image

Make it make sense

Only the context you need to fix your broken code with Sentry.

Start debugging →

Top comments (0)

AWS Security LIVE! Stream

Go beyond the firewall

There's more to security than code. Explore solutions, strategies, and the full story on AWS Security LIVE!

Learn More

👋 Kindness is contagious

Dive into this insightful write-up, celebrated within the collaborative DEV Community. Developers at any stage are invited to contribute and elevate our shared skills.

A simple "thank you" can boost someone’s spirits—leave your kudos in the comments!

On DEV, exchanging ideas fuels progress and deepens our connections. If this post helped you, a brief note of thanks goes a long way.

Okay