[英]What changes were done in a form using accepts_nested_attributes_for
Regarding this article: tracking activerecord objects 关于本文: 跟踪activerecord对象
I need to know what fields are changed in a big form using accepts_nested_attributes_for. 我需要知道使用accepts_nested_attributes_for会以何种形式大幅度更改哪些字段。
Currently the form works well as expected. 目前,该表格可以正常运行。 But in addition I'm doing a "log history" about user's changes.
但是此外,我正在做有关用户更改的“日志历史记录”。
I have tried the hash mapping, but is really complicated to me since the models aren't small, and talking about the above article maybe exists a better way to track changes. 我已经尝试了哈希映射,但是由于模型不小,这对我来说确实很复杂,谈论上面的文章可能是跟踪更改的更好方法。
My models are (in case is necessary): 我的模型是(如果有必要):
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
(models are very normal). (模型非常正常)。
Any ideas?, and if I'm forced to map the hash, have you a little sample about how to ? 有任何想法吗?如果我被迫映射散列,您是否有一些有关如何做的示例?
Thanks in advance 提前致谢
After several options (thank's Beerlington) with same above article I did I wanted: 在对上面的文章进行了几种选择(谢谢的比林顿)之后,我想要做的是:
Read this: ActiveRecord::Dirty 阅读: 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
The important things in the procedure are: 该过程中的重要内容是:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.