簡體   English   中英

使用accepts_nested_attributes_for對表單進行了哪些更改

[英]What changes were done in a form using accepts_nested_attributes_for

關於本文: 跟蹤activerecord對象

我需要知道使用accepts_nested_attributes_for會以何種形式大幅度更改哪些字段。

目前,該表格可以正常運行。 但是此外,我正在做有關用戶更改的“日志歷史記錄”。

我已經嘗試了哈希映射,但是由於模型不小,這對我來說確實很復雜,談論上面的文章可能是跟蹤更改的更好方法。

我的模型是(如果有必要):

class Customer < ActiveRecord::Base
  has_many   :addresses

  attr_accessible :nom, :prenom, :langue, :nationalite, :codeFiscal, :hidden_status, :subscribed
  attr_accessible :addresses_attributes, allow_destroy: true

  accepts_nested_attributes_for :addresses
end


class Address < ActiveRecord::Base
  belongs_to :customer
  has_many   :telephones

  attr_accessible :flag, :societe, :titre, :persContact, :rue, :rue1, :nopostal, :lieu, :pays
  attr_accessible :hidden_status
  attr_accessible :telephones_attributes

  accepts_nested_attributes_for :telephones, :reject_if => :all_blank, :allow_destroy => true
end


class Telephone < ActiveRecord::Base
  belongs_to :address

  attr_accessible :typeNumero, :numeroTel
end

(模型非常正常)。

有任何想法嗎?如果我被迫映射散列,您是否有一些有關如何做的示例?

提前致謝

在對上面的文章進行了幾種選擇(謝謝的比林頓)之后,我想要做的是:

閱讀: ActiveRecord :: Dirty

def update
    @customer = Customer.find(params[:id])
    @customer.assign_attributes(params[:customer])
    if @customer.valid?
      # If changued I revise the record in the model
      @customer = @customer.requires_log if @customer.changed?
      # and very useful: @customer.changes gives you an array con every changue
      @customer.save
    end
    # similar for addresses and phones but into a loop:
    # @customer.addresses.each do |address|   ...  end
    # I didn't put it because it is repetitive
  end

該過程中的重要內容是:

  1. 要應用assign_attributes而不是保存,以在保存之前跟蹤更改。
  2. 在這種情況下適用有效嗎? 只追蹤正確的變化。
  3. 處理變更,然后保存等。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM