2011-02-05 45 views
0

在腳手架的Rails 3中,當'update'方法無法保存時,邏輯已經存在,可以重定向回編輯頁面。rails 3,scaffolded app,如何添加自己的錯誤信息更新方法?

我們修改了腳手架的方法做一些定製的驗證邏輯(更新後,但我們呈現結果視圖

respond_to do |format| 
    if @thingy.update_attributes(params[:thingy]) 
     if @thingy.found_warning_101 

      WHAT GOES "HERE" TO REDIRECT TO EDIT PAGE 
      AND HAVE THE DEFAULT SCAFFOLDING ERROR HANDLING SHOW "WARNING 101"? 
      THIS DOES NOT WORK, GIVES MISSING VIEW ERROR, DOESNT FIND EDIT VIEW: 

      format.html { render :action => "edit", :notice => "WARNING 101" } 
      format.xml { render :xml => @things.errors, :status => :unprocessable_entity } 
      return 

     end 

    format.html { redirect_to(@thingy, :notice => "thingy was successfully updated.") } 
    format.xml { head :ok } 
    else 
    format.html { render :action => "edit" } 
    format.xml { render :xml => @beep.errors, :status => :unprocessable_entity } 
    end 

之前我們嘗試(上)簡單地複製腳手架創建的情況下相同的代碼該.update_attributes失敗(依次是返回),但我們得到一個丟失的觀點錯誤:

Missing template thingys/update with {:handlers=>[:erb, :rjs, :builder, :rhtml, :rxml, :haml, :voice], :formats=>[:html], :locale=>[:en, :en]} in view paths 

回答

1

我真的不明白你爲什麼要這麼做,反正這裏是實現它的方式:

@thingy.errors[:base] << "whatever text you want" 

當然在你的控制器。

順便說一句,缺少的模板似乎update,不edit

+0

是的,這是一種愚蠢的形勢下,我們正在處理。感謝有關添加錯誤消息的信息。我不明白爲什麼render:action =>「edit」會拋出該錯誤...對於update_attributes的處理已經被調用了,我想呢?我的猜測是我們需要重定向語句重新導向到更新方法再次...不知道該怎麼做,雖然 – jpwynn 2011-02-05 23:44:44