2011-06-09 103 views
0

我正在開發一個帶有Ruby 1.8.7和Rails 2.3.5的webapp,它將管理一些工業過程。其中一個要求是,當有人想要刪除註冊表時,刪除任務將不得不更新數據庫表的某個字段。該欄位確定記錄是否有效。更新而不是刪除

我已修改的默認刪除由支架生成的方法:

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 

其中:estado_id => 1是活動的,並且是:estado_id => 2去激活。但是,當我嘗試執行更新時,它不會更改該屬性。其實,它什麼都不做。

有沒有人有線索?

+2

請按照Rails慣例。調用你的字段'is_active'並簡單地將它設置爲true或false。 – meagar 2011-06-09 20:02:10

+4

@meager:「Rails約定」可能會將該字段稱爲「active」,並且是一個布爾值,不需要「is」前綴:) – 2011-06-09 21:03:56

回答

0

你應該在你的情況

update_attributes方法(屬性)公共

更新所有從 傳入的哈希屬性使用update_attributes並保存記錄。 如果對象無效,則保存 將失敗並返回false。

例如:

,或者如果你想使用update_attribute

update_attribute(名稱,值)公共

@alarma.update_attribute(:estado_id, 2)更新單個屬性並保存 記錄,而不通過 正常驗證程序。

相關問題