2012-02-02 51 views
0

在引號控制器中保存新報價時,我們的rails 3.1.0應用程序中存在一個奇怪的錯誤。一個報價屬於rfq,rfq是18被傳入報價控制器。錯誤是:輸入的整數在軌道3.1.0中不被視爲數字

Validation failed: Quoted total is not a number 

顯示的參數是:

{"utf8"=>"✓", 
"authenticity_token"=>"p9waUq7wNk6djm9uRtDOA0eHLzzlJuSCWrTsSEBvcck=", 
"quote"=>{"test_item_ids"=>["1", 
"2"], 
"_destroy"=>"false", 
"quoted_total"=>"290"}, 
"commit"=>"Save", 
"rfq_id"=>"18"} 

,因爲它表明,在quoted_total是290.但不知何故,它無法識別爲一個數字。

這裏是在引號控制器創建代碼:

@quote = @rfq.quotes.new(params[:quote], :as => :roles_new) 
    @quote.input_by_id = session[:user_id] 
    @quote.test_items = TestItem.find_all_by_id(params[:quote][:test_item_ids]) 
    if @quote.save! 
     redirect_to URI.escape("/view_handler?index=0&msg=quote saved!") 
    else 
     flash.now[:error] = "Not saved!" 
     render 'new' 
    end 

錯誤是由@ quote.save引起的。引用控制器的rspec代碼具有相同的錯誤。什麼導致了錯誤?非常感謝。

回答

1

我想你在你的模型中驗證quoted_total是一個數字,事實上,params [:quoted_total]是一個字符串。

+0

yes,as:validates_numericality_of:quoted_total,:greater_than => 0.00。但是由params返回的所有數據都是字符串,模型知道如何轉換它們。 – user938363 2012-02-02 05:20:09

+0

發現問題了!它是由attr_accessible中的錯誤名稱引起的。 quoted_total不在attr_accessible中,因此它的值不能被傳入。 – user938363 2012-02-02 05:23:44

相關問題