使用fields_for的Rails错误消息 [英] Rails error message using fields_for

查看:84
本文介绍了使用fields_for的Rails错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的子分类帐会计应用程序中,我有一个资金模型

In my sub-ledger accounting rails app I have a funds model

class Fund < ActiveRecord::Base
    belongs_to :agency
    has_many :gl_accounts

    accepts_nested_attributes_for :gl_accounts

    attr_accessible :name, :agency_id, :fund, :user_stamp, :active
    attr_accessible :gl_accounts_attributes

和gl_accounts模型

and a gl_accounts model

class GlAccount < ActiveRecord::Base
    belongs_to :agency
    belongs_to :fund
    has_many :class_sessions
    has_many :facilities

    validates :agency_id, :fund_id, :name, :gl_account_number, :active, :user_stamp, :account_type, :presence => true
    validates_uniqueness_of :account_type, :scope => :fund_id, :if => :unique_account_type

    attr_accessible :agency_id, :fund_id, :name, :gl_account_number, :active, :user_stamp, :account_type

    def unique_account_type
        [3,4,6,7,8].include? account_type
    end

创建新基金时,必须同时创建5个gl_account,因此,当为基金创建新记录时,我将使用fields_for在gl_account模型中创建5个新记录.直到我提交表单,并且收到错误消息说"GL帐户资金不能为空",一切似乎都可以正常工作.

When a new fund is created there are 5 gl_accounts that must be created at the same time so I am using fields_for to create the 5 new records in the gl_account model when the new record is created for fund. It all seems to work OK until I submit the form and I get an error saying that "Gl accounts fund cannot be blank."

gl_accounts模型上没有基金"属性.我以为Rails可能会删除"_id"部分(因为有一个fund_id外键字段),但是我的理解是,使用嵌套模型和fields_for会自动在fund_id字段中添加适当的值( gl_account模型).但是,即使我在表单中添加一个值为fund_id的隐藏字段,我仍然会收到错误消息,说基金"不能为空.

There is no "fund" attribute on the gl_accounts model. I thought that maybe rails was dropping the "_id" part (since there is a fund_id foreign key field) but I was under the understanding that using nested models and fields_for automatically added the proper value in the fund_id field (the foreign key of the gl_account model). But even if I add a hidden field in the form with a value for fund_id I still get the error saying that "fund" cannot be blank.

那么,也许Rails试图告诉我我还有其他问题吗?

So, maybe rails is trying to tell me I have something else wrong?

这里是参数:

{"utf8"=>"✓",
 "authenticity_token"=>"MNWLFOnLOE+ZRsUf9mogf2cq/TeQ+mxtrdaVu3bEgpc=",
 "fund"=>{"agency_id"=>"1",
 "user_stamp"=>"6",
 "name"=>"Junk",
 "fund"=>"44",
 "active"=>"1",
 "gl_accounts_attributes"=>{"0"=>{"agency_id"=>"1",
 "user_stamp"=>"6",
 "account_type"=>"6",
 "name"=>"Cash Account",
 "active"=>"1",
 "fund_id"=>"1",
 "gl_account_number"=>"44-498-965-789"},
 "1"=>{"agency_id"=>"1",
 "user_stamp"=>"6",
 "account_type"=>"7",
 "name"=>"Credit Card Account",
 "active"=>"1",
 "fund_id"=>"1",
 "gl_account_number"=>"44-498-965-163"},
 "2"=>{"agency_id"=>"1",
 "user_stamp"=>"6",
 "account_type"=>"3",
 "name"=>"Customer Account Balances",
 "active"=>"1",
 "fund_id"=>"1",
 "gl_account_number"=>"44-498-965-254"},
 "3"=>{"agency_id"=>"1",
 "user_stamp"=>"6",
 "account_type"=>"8",
 "name"=>"Refunds Pending Account",
 "active"=>"1",
 "fund_id"=>"1",
 "gl_account_number"=>"44-498-965-456"},
 "4"=>{"agency_id"=>"1",
 "user_stamp"=>"6",
 "account_type"=>"4",
 "name"=>"Deferred Revenue Account",
 "active"=>"1",
 "fund_id"=>"1",
 "gl_account_number"=>"44-498-965-159"}}},
 "commit"=>"Add New Fund"}

推荐答案

尝试从GlAccount类中的状态真实验证中删除fund_id.

Try removing fund_id from the presence true validation in GlAccount class.

validates :agency_id, :name, :gl_account_number, :active, :user_stamp, :account_type, :presence => true

也不要添加fund_id作为隐藏字段,因为,是的,'fields_for'将自动处理该问题,但这将在验证后发生.

And also don't add fund_id as hidden field because , you are right, 'fields_for' will automatically take care of that but that will happen after validations.

因此,您不需要验证fund_id的状态即可.

So you don't need fund_id to be validated for presence.

更新

此外,要确保fund_id永远不会为null,可以在数据库表中放置一个约束.使用以下代码创建迁移.

Also to ensure that fund_id is never null you can put a constraint in database table. Create a migration with the following code.

change_column :gl_accounts, :fund_id, :integer, :null => false

更新2

要确保存在资金,您需要检查是否存在资金,而不是fund_id.

To ensure that fund is there you need to check for presence of fund not fund_id.

validates :fund, :presence => true

为此,您需要使用"inverse_of"声明关联,如下所示.

And for this to work you need to declare your associations with 'inverse_of' like below.

class Fund < ActiveRecord::Base
  has_many :gl_accounts, inverse_of: :fund
  accepts_nested_attributes_for :gl_accounts
end

class GlAccount < ActiveRecord::Base
  belongs_to :fund, inverse_of: :gl_accounts
  validates_presence_of :fund
end

有关更多详细信息,请参阅本指南. http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html#label-Validating+the+presence+of+a+parent+model

For more details please refer this guide. http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html#label-Validating+the+presence+of+a+parent+model

这篇关于使用fields_for的Rails错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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