Rails: checking modified fields -


First of all, I have created a scuffold called 'item'

I want to check who From the field item has been modified and I have tried two possible endeavors, they are not working either.

The first attempt!

  def edits @item = Item.find (params [: id]) @item_before_update = @ item.dup enddef update @ item = item.fid (param [: id]) # @ Item_before_update and @item compare here, but @item_before_update NIL !!! End  

The second attempt! I see the way I can not move data to the controller and I could not. edit.html.erb

  & lt;% @item_before_update = @ item.dup% & gt; # I thought @item_before_update can be read in the item control's updated method. But not & Lt;% params [: item_before_update] = @ item.dup% & gt; # And I also thought that the parameter [: item_before_update] can be read in the update mothod of the item controller. But not again & lt;% form_for (@item) do. F | | & Gt%; #excluded & lt;% end% & gt;  

Please tell me how to solve this problem: (

< P> In those properties whose changes have been retained, have they changed ? @ item.title_changed?

You use changed

  @ item.changed # => ['title']   You can get an array of changed attributes by  #update  for action, you need to use  attributes =  to change the object You can then use  changed  and  has changed?  before continuing: 

  def update @item = Item.find (params [: id]) @ item.attributes = params [: items] @item changed # = & gt; ['title'] ... stuff @ item.save end  

Comments

Popular posts from this blog

sql - dynamically varied number of conditions in the 'where' statement using LINQ -

asp.net mvc - Dynamically Generated Ajax.BeginForm -

Debug on symbian -