【问题标题】:Error on updating nested attributes in rails更新 Rails 中的嵌套属性时出错
【发布时间】:2015-09-27 20:52:05
【问题描述】:

三种型号:

class Order < ActiveRecord::Base
  belongs_to :product
  belongs_to :sale
end

class Sale < ActiveRecord::Base
  has_many :orders
  accepts_nested_attributes_for :orders, :reject_if => :all_blank
end

class Product < ActiveRecord::Base
  belongs_to :greenhouse
  has_many :orders
end

首先创建一个产品。然后,可以用一个产品下订单。然后,我期望的是,销售充满了许多订单。但是,在保存销售时,它只会忽略附加的订单。

我只找到了Sale创建Order,或者父对象创建Child对象的例子,但是这种情况下,子对象,或者Order已经创建,只需要赋值或者关联新的销售。

如何将子对象与新的父对象关联起来?

【问题讨论】:

  • 你能把出错的代码显示出来吗?

标签: ruby-on-rails ruby-on-rails-4 nested-attributes nested-form-for


【解决方案1】:

请确保您的 SalesController 有许可参数,如下所示:

params.require(:sale).permit(:each, :sale, :field, :as, :symbol, :and, orders_attributes: [:each, :order, :field, :as, :symbol])

这是给出所描述行为的最常见问题。如果没有,我们需要更多信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    相关资源
    最近更新 更多