如何在Rails 3中验证非模型表单? [英] How do I validate a non-model form in Rails 3?

查看:136
本文介绍了如何在Rails 3中验证非模型表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个搜索表单,我想验证关键字的长度。我希望表单突出显示无效字段(如模型表单)。我已到处寻找,似乎无法找到有关在Rails 3中验证非模型支持的表单的任何有用信息。我试过了 validatable gem,但这似乎不起作用使用Rails 3:

 用于Login:Class的未定义方法`model_name'。 

有什么想法?

解决方案

使用Rails 3,你可以使用诸如Validations等一些
ActiveModel扩展来扩展普通旧Ruby对象。ActiveRecord是ActiveModel的扩展,支持DB。这里有一个很好的Railscast,用简单的例子来描述它:


在Rails 3中,Active Record的非数据库
功能是
被提取到主动模型中。这
允许您干净地添加验证
和其他功能无表
模型。 http://railscasts.com/episodes/219-active-model



Let's say I have a search form and I want to validate the length of the keyword. I want the form to highlight the invalid field (like model forms). I've looked everywhere and can't seem to find any useful information on validating non-model-backed forms in Rails 3. I've tried the validatable gem, but this doesn't seem to work with Rails 3:

undefined method `model_name' for Login:Class.

Any ideas?

解决方案

With Rails 3 you can extend "plain old Ruby objects" with some ActiveModel extensions like Validations etc. ActiveRecord is extension of ActiveModel with DB support. Here is a very good Railscast describing it with easy example:

In Rails 3 the non-database functionality of Active Record is extracted out into Active Model. This allows you to cleanly add validations and other features to tableless models. http://railscasts.com/episodes/219-active-model

这篇关于如何在Rails 3中验证非模型表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆