热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

ValidationinRailswithoutamodel

Ihaveaformthatallowstheusertosendamessagetoanemail,andIwanttoaddvalidationtoit

I have a form that allows the user to send a message to an email, and I want to add validation to it. I do not have a model for this, only a controller. How should I do this in Rails?

我有一个允许用户向电子邮件发送消息的表单,我想为其添加验证。我没有这个模型,只有一个控制器。我应该如何在Rails中执行此操作?

I was considering doing the validation in the controller, and displaying the errors to the user using the flash object. Is there a better way of doing this?

我正在考虑在控制器中进行验证,并使用flash对象向用户显示错误。有更好的方法吗?

2 个解决方案

#1


6  

The best approach would be to wrap up your pseudo-model in a class, and add the validations there. The Rails way states you shouldn't put model behavior on the controllers, the only validations there should be the ones that go with the request itself (authentication, authorization, etc.)

最好的方法是在类中包装伪模型,并在那里添加验证。 Rails方式声明你不应该在控制器上放置模型行为,唯一的验证应该是与请求本身一起的(验证,授权等)。

In Rails 2.3+, you can include ActiveRecord::Validations, with the little drawback that you have to define some methods the ActiveRecord layer expects. See this post for a deeper explanation. Code below adapted from that post:

在Rails 2.3+中,您可以包含ActiveRecord :: Validations,您必须定义ActiveRecord层期望的某些方法。有关更深入的解释,请参阅此文章。以下代码改编自该帖子:

require 'active_record/validations'

class Email

  attr_accessor :name, :email
  attr_accessor :errors

  def initialize(*args)
    # Create an Errors object, which is required by validations and to use some view methods.
    @errors = ActiveRecord::Errors.new(self)
  end

  # Required method stubs
  def save
  end

  def save!
  end

  def new_record?
    false
  end

  def update_attribute
  end

  # Mix in that validation goodness!
  include ActiveRecord::Validations

  # Validations! =)
  validates_presence_of :name
  validates_format_of :email, :with => SOME_EMAIL_REGEXP
end

In Rails3, you have those sexy validations at your disposal :)

在Rails3中,您可以使用这些性感验证:)

#2


2  

For Rails 3+, you should use ActiveModel::Validations to add Rails-style validations to a regular Ruby object.

对于Rails 3+,您应该使用ActiveModel :: Validations将Rails样式的验证添加到常规Ruby对象。

From the docs:

来自文档:

Active Model Validations

Provides a full validation framework to your objects.

为您的对象提供完整的验证框架。

A minimal implementation could be:

最小的实现可能是:

class Person
  include ActiveModel::Validations

  attr_accessor :first_name, :last_name

  validates_each :first_name, :last_name do |record, attr, value|
    record.errors.add attr, 'starts with z.' if value.to_s[0] == ?z
  end
end

Which provides you with the full standard validation stack that you know from Active Record:

它为您提供了从Active Record中获知的完整标准验证堆栈:

person = Person.new
person.valid?                   # => true
person.invalid?                 # => false

person.first_name = 'zoolander'
person.valid?                   # => false
person.invalid?                 # => true
person.errors.messages          # => {first_name:["starts with z."]}

Note that ActiveModel::Validations automatically adds an errors method to your instances initialized with a new ActiveModel::Errors object, so there is no need for you to do this manually.

请注意,ActiveModel :: Validations会自动为使用新ActiveModel :: Errors对象初始化的实例添加errors方法,因此您无需手动执行此操作。


推荐阅读
author-avatar
訫嬘風飛_487_519
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有