I’m developing a webapp with Ruby 1.8.7 and Rails 2.3.5, which will manage some industrial process. One of the requirements is that, when someone wants to delete a registry, the delete task will have to make an update to one of the fields of the DB table. This field establishes if a record is active or not.
I have modified the default delete method generated by the scaffold:
def activar
@alarma = Alarma.find(params[:id])
respond_to do |format|
if @alarma.update_attribute(:estado_id => 1)
flash[:notice] = 'La alarma ha sido activada.'
format.html { redirect_to(@alarma) }
format.xml { head :ok }
else
format.html { render :action => "index" }
format.xml { render :xml => @alarmas }
end
end
end
def desactivar
@alarma = Alarma.find(params[:id])
respond_to do |format|
if @alarma.update_attribute(:estado_id => 2)
flash[:notice] = 'La alarma fue desactivada.'
format.html { redirect_to(@alarma) }
format.xml { head :ok }
else
format.html { render :action => "index" }
format.xml { render :xml => @alarmas }
end
end
end
where :estado_id => 1 is active, and :estado_id => 2 is deactive. However, when a I try to do the update, it doesn’t change the attribute. Actually, it doesn’t do anything.
Does anyone have a clue?
You should use update_attributes in your case
example:
or
@alarma.update_attribute(:estado_id, 2)if you want to use update_attribute