Frontend vs Backend validations | Ruby on Rails

时间:2015-07-31 20:16:35

标签: ruby-on-rails activerecord

When is a better to use js/frontend and ActiveRecord/backend validatons?

After watching various RailsCasts I came up with a thought that I should reduce backend requests as much as possible. Is that right?

Thanks!

2 个答案:

答案 0 :(得分:0)

Anything that must be trusted must be done at the backend. Everything coming from the browser can be manipulated, regardless of your validations there.

Use JS/frontend validation for validations that make data entry easier for the user.

Reducing backend requests is helpful, but stopping vandals from putting garbage in will require backend checks.

答案 1 :(得分:0)

You can't actually compare these two and tell which on is better. Frontend validations are GOOD for user experience and reducing server load, and backend validations are MUST because they are responsible for validity of your data. But you can't rely on front end validations.

In general, I would prefer client_side_validations gem that allows you to use model validations on frontend. It can handle most of the frequent validations and some custom validations as well, so you don't have to duplicate a lot of validation rules on the frontend.