ruby-on-rails – Rails:检查已修改的字段
发布时间:2020-12-16 20:09:18 所属栏目:百科 来源:网络整理
导读:首先,我生成了称为“项目”的脚手架 我想检查项目的哪些字段被修改.我尝试过两次可能的尝试,那些不行. 第一次尝试! def edit @item = Item.find(params[:id]) @item_before_update = @item.dupenddef update @item = Item.find(params[:id]) # compare @ite
首先,我生成了称为“项目”的脚手架
我想检查项目的哪些字段被修改.我尝试过两次可能的尝试,那些不行. 第一次尝试! def edit @item = Item.find(params[:id]) @item_before_update = @item.dup end def update @item = Item.find(params[:id]) # compare @item_before_update and @item here,but @item_before_update is NIL !!! end 二次尝试! <% @item_before_update = @item.dup %> # I thought @item_before_update can be read in update method of item controller. But NO. <% params[:item_before_update] = @item.dup %> # And I also thought params[:item_before_update] can be read in update mothod of item controller. But AGAIN NO <% form_for(@item) do |f| %> # omitted <% end %> 请让我知道如何解决这个问题:( 解决方法
具有尚未持久变化的属性将会更改为真?
@item.title_changed? #=> true 您可以通过使用更改来获取已更改属性的数组 @item.changed #=> ['title'] 对于#update操作,您需要使用attributes =来更改对象,那么可以使用更改和更改?之前坚持: def update @item = Item.find(params[:id]) @item.attributes = params[:item] @item.changed #=> ['title'] ... do stuff @item.save end (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |