且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Ruby on Rails:使用一种表单和一种提交方式为多个模型创建记录

更新时间:2023-11-24 10:41:04

要提交表单及其关联的子级,您需要使用

To submit a form and it's associated children you need to use accepts_nested_attributes_for

为此,您需要在要使用的控制器的模型上对其进行声明(在您的情况下,它看起来像是Quote Controller.

To do this, you need to declare it at the model for the controller you are going to use (in your case, it looks like the Quote Controller.

class Quote < ActiveRecord::Base
  attr_accessible :quote_number
  has_one :customer
  has_one :item
  accepts_nested_attributes_for :customers, :items
end

此外,您需要确保声明可以访问的属性 您可以避免其他批量分配错误.

Also, you need to make sure you declare which attributes are accessible so you avoid other mass assignment errors.