Rails验证不在嵌套模型上运行

Rails验证不在嵌套模型上运行

本文介绍了Rails验证不在嵌套模型上运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Rails 3.2.8和Ruby 1.9.3上。



我无法弄清楚为什么嵌套属性的验证没有运行或返回任何错误。当我提交没有填写的表单时,我会为父模型(User)返回错误,但不会为子模型(Account)返回错误。在我的代码中下面,我有一个has_one owned_account(帐户模型)的User模型和一个属于所有者(用户模型)的Account模型。帐户模型有一个子域字符串的文本字段。



似乎当我提交表单时不包括子域字段,帐户模型上的验证不运行在所有。关于如何获得验证的任何想法在这里工作?预先感谢您提供任何帮助或指示。



user.rb

  class User< ActiveRecord :: Base 
attr_accessible:owned_account_attributes
has_one:owned_account,:class_name => 'Account',:foreign_key => 'owner_id'

validates_associated:owned_account
accep_nested_attributes_for:owned_account,:reject_if => proc {|属性|属性[的子域。空白? }
结束

account.rb

  class Account< ActiveRecord :: Base 
attr_accessible:owner_id,::subdomain
belongs_to:owner,:class_name => 'User'

验证:subdomain,
:presence =>真,
:唯一性=> true,
:format => {...一些代码...}
end

new.haml

  = form_for @user do | f | 
...与用户相关的字段...
= f.fields_for:owned_account_attributes do | acct |
= acct.label:subdomain
= acct.text_field:subdomain
= submit_tag ...

users_controller.rb

  class UsersController< ApplicationController 
def new
@user = User.new
end
$ b def create
@user = User.new(params [:user])

if @ user.save
...
end
end

$ b $你需要在用户模型中添加 acceptable_nested_attributes_for 方法。像这样:

  class User< ActiveRecord :: Base 
attr_accessible:owned_account_attributes,#其他用户属性
has_one:owned_account,:class_name => 'Account',:foreign_key => 'owner_id'

accept_nested_attributes_for:owned_account
validates_associated:owned_account
end

然后您应该看到与父模型(User)上的嵌套模型有关的验证错误:

  [拥有的帐户子域不能为空,拥有的帐户无效] 

编辑
$ b 罪魁祸首原来是:reject_if 如果子域属性是空白的话,有效地指示Rails忽略嵌套的帐户对象(请参阅评论中的讨论)


I'm on Rails 3.2.8 and Ruby 1.9.3.

I'm having trouble figuring out why the validations on the nested attributes are not being run or returning any errors. When I submit the form with nothing filled in, I get errors back for the parent model (User), but not for the child model (Account).

In my code below, I have a User model which has_one owned_account (Account model), and an Account model that belongs_to an owner (User model). The Account model has a text field for a subdomain string.

It seems that when I submit the form without including the subdomain field, the validations on the Account model are not run at all. Any ideas on how I can get the validations here working? Thanks in advance for any help or pointers.

user.rb

class User < ActiveRecord::Base
  attr_accessible :owned_account_attributes
  has_one :owned_account, :class_name => 'Account', :foreign_key => 'owner_id'

  validates_associated :owned_account
  accepts_nested_attributes_for :owned_account, :reject_if => proc { |attributes| attributes['subdomain'].blank? }
end

account.rb

class Account < ActiveRecord::Base
  attr_accessible :owner_id, :subdomain
  belongs_to :owner, :class_name => 'User'

  validates :subdomain,
    :presence => true,
    :uniqueness => true,
    :format => { ...some code... }
end

new.haml

= form_for @user do |f|
  ... User related fields ...
  = f.fields_for :owned_account_attributes do |acct|
    = acct.label :subdomain
    = acct.text_field :subdomain
  = submit_tag ...

users_controller.rb

class UsersController < ApplicationController
  def new
    @user = User.new
  end

  def create
    @user = User.new(params[:user])

    if @user.save
      ...
    end
end
解决方案

You need to add the accepts_nested_attributes_for method to the User model. Like so:

class User < ActiveRecord::Base
  attr_accessible :owned_account_attributes, # other user attributes
  has_one :owned_account, :class_name => 'Account', :foreign_key => 'owner_id'

  accepts_nested_attributes_for :owned_account
  validates_associated :owned_account
end

Then you should see validation errors pertaining to the nested model on the parent model (User):

["Owned account subdomain can't be blank", "Owned account is invalid"]

EDIT

The culprit turned out to be the :reject_if bit in the accepts_nested_attributes_for line that effectively instructed Rails to ignore nested account objects if the subdomain attribute was blank (see discussion in comments)

这篇关于Rails验证不在嵌套模型上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 06:46