开发者

Validating throwaway forms not related to a model

开发者 https://www.devze.com 2023-02-18 07:06 出处:网络
I have a fo开发者_Python百科rm which takes in data, but it is not stored in any model. Instead, it is used to perform calculations and modify a model.Is there any way I can validate the data on the fo

I have a fo开发者_Python百科rm which takes in data, but it is not stored in any model. Instead, it is used to perform calculations and modify a model. Is there any way I can validate the data on the form (for example, forcing certain inputs to be of integer form) within rails, and without writing it manually in javascript?


Because of the rails mantra of "keep your controllers skinny", you should put your validations in your models. However you are stating that this data isn't backed by a database. This is ok, since you can create objects that act like a model(which means you can still have the activerecord validations in there).

I would highly suggest reading Yehuda Katz's post on ActiveModel: Make Any Ruby Object Feel Like ActiveRecord. From there you can use the ActiveRecord validations that you've been used to!

0

精彩评论

暂无评论...
验证码 换一张
取 消