如何验证Rails 4中的嵌套属性字段? [英] how can i validate the nested attributes field in rails 4?

查看:65
本文介绍了如何验证Rails 4中的嵌套属性字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个模型

class Information < ActiveRecord::Base
  belongs_to :study
  validates_presence_of :email
end

class Study < ActiveRecord::Base
  has_many :informations
  accepts_nested_attributes_for :informations
end

我展示了一种学习形式,其中包含很少的信息领域,并且我想验证这些领域的存在.仅在验证成功时,我也要保存研究字段值,并且如果验证失败,我想显示错误.我怎样才能做到这一点?预先感谢.

I show up a form of study which contains few fields for the informations and i want to validate presence of those fields. Only on validation success i wanted to save the study field values as well and i wanted to show errors if the validation fails. How can i do this? Thanks in advance.

推荐答案

通常,您可以在所需的模型中编写验证.因此,如果您需要验证Information类中字段foo的存在,则只需在该类中编写validates_presence_of :foo即可.同样,对Study字段的验证仅在Study类中进行.使用嵌套属性,当您从包含嵌套属性的params哈希更新Study实例时,它也会同时更新Information实例,并在运行过程中进行验证.这就是accepts_nested_attributes_for调用正在做的事情-为"params"散列的适当位提供权限",以这种方式使用.

You write validations in the models that you require, as normal. So if you need to validate presence of field foo in the Information class you'd just write validates_presence_of :foo in that class. Likewise validations for Study fields just go in the Study class. With nested attributes, when you update a Study instance from a params hash that contains nested attributes, it'll update the Information instance(s) too, running validations in passing. That's what the accepts_nested_attributes_for call is doing - it's giving "permission" for the appropriate bits of a params hash to be used in this way.

您可以使用reject_if仅拒绝 new 嵌套记录(如果它们不符合条件).因此,我可以让某人创建一个研究并仅创建一个或多个与该研究相关联的嵌套信息实例,前提是他们填写了表格中的字段,但是如果他们将它们留为空白,则不会创建嵌套的内容并保存(这样就不会得到毫无意义的空白关联记录).研究仍将保存.例如:

You can use reject_if to only reject new nested records should they fail to meet criteria. So I might let someone create a Study and only create one or more nested Information instances associated with that Study if they'd filled in field(s) in the form, but if they left them blank, the nested stuff wouldn't be created and saved (so you don't get pointless blank associated records). The Study would still be saved. For example:

accepts_nested_attributes_for(
  :informations,
  reject_if: proc() { | attrs | attrs[ 'title' ] .blank? }
)

此处的API文档涵盖了更多内容:

This and more is covered in the API documentation here:

请注意,嵌套字段仅用于现有记录.如果您要在new/create操作中创建一个 new 研究实例,而没有关联任何Information实例,则您根本不会看到Information类的任何嵌套表单字段-当您可能期望只有一个,用于空白的新项目.如果您还没有准备好,那可能会非常令人困惑!您需要在控制器中的Study实例中手动添加一个新的Information实例,或类似的操作来执行新建"和创建"操作,例如使用before_filter :create_blank_object, only: [ :new, :create ],例如:

Beware that nested fields are intended for existing records only. If you were creating a new Study instance in a new/create action with no Information instances associated, you won't see any nested form fields for your Information class at all - when you might be expecting just one, for a blank new item. This can be very confusing if you aren't ready for it! You'll need to manually add a new Information instance to your Study instance in the controller or similar for the 'new' and 'create' actions, e.g. using before_filter :create_blank_object, only: [ :new, :create ], with, say:

def create_blank_object
  @study = Study.new
  @study.informations << Information.new
end

这篇关于如何验证Rails 4中的嵌套属性字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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