2011-09-06 44 views
6

我有我的模型內的一些標準軌驗證:導軌不產生驗證失敗的消息

validates_presence_of :url_string 
validates_uniqueness_of :url_string 
validates_presence_of :stream_source 
validates_presence_of :width 
validates_presence_of :height 
validates_presence_of :name 
validates_uniqueness_of :name 
validates_presence_of :customer_name 
validates_presence_of :iframe_background_color 

如果我不填寫自己的狀態,然後我將返回到表單中這些字段之一如預期的那樣,但奇怪的是沒有顯示錯誤消息。我使用下面的代碼來顯示錯誤消息:

<% @camera.errors.full_messages.each do |error| %> 
    <p><%= error %></p> 
<% end % 

我也試圖打印出@ camera.errors對象,這是什麼所示:

#<ActiveModel::Errors:0x12db19bc @base=#<Camera id: 1, stream_source: "test", width: 640, height: 360, active: true, name: "test", url_string: "CAYD19Vp", customer_name: "test", iframe_background_color: "#FFFFFF", online: true, created_at: "2011-08-30 15:54:16", updated_at: "2011-09-06 15:52:48", audio: true, iframe_text_color: "#FF00FF", iframe_link_color: "#FF0000", notes: "Some notes!", offline_image_file_name: "Cake.jpg", offline_image_content_type: "image/jpeg", offline_image_file_size: 196591, offline_image_updated_at: "2011-09-06 12:12:38", pull_stream_url: "test", bitrate: "300-500", show_branding: false>, @messages={}> 

正如你所看到的消息散列是空的。我嘗試通過執行以下操作手動設置驗證錯誤消息:

validates_presence_of :name, :message => "No name present" 

但它沒有填充消息散列。

控制器更新操作如下圖所示:

def update 
    @camera = Camera.find(params[:id]) 
    if @camera.update_attributes(params[:camera]) 
    flash[:notice] = "Camera updated" 
    redirect_to nwcadmin_camera_path 
    else 
    redirect_to :action => :edit 
    end 
end 

我使用Ruby版本紅寶石1.9.2p290和Rails 3.1.0版本。

任何援助將是偉大的!

感謝

+1

控制器代碼在這裏可能會有所幫助。 – jdl

+0

謝謝,我添加了控制器更新操作。 – stephenheron

+0

我設法解決了我的問題,但由於我無法回答自己的問題7個小時,我暫時將解決方案放在這裏。在控制器我使用: redirect_to的:行動=>:編輯 我應該已經使用: 渲染:行動=>:編輯 通過使用redirect_to的我被擊中,然後將其在控制器內的編輯操作從數據庫中獲取新的相機對象,而不是從更新操作保留相機對象。 – stephenheron

回答

5

我設法讓我的問題的底部。在控制器我用:

redirect_to :action => :edit 

我應該一直在使用:

render :action => :edit 

通過使用redirect_to時我打的控制器中,然後從數據庫中獲取一個新的相機對象中的編輯操作而不是從更新操作中保留當前的相機對象。

2

除非你叫@camera.save@camera.valid?,錯誤哈希不會與驗證錯誤填充。請檢查您的控制器代碼。

+0

update_attributes確實運行驗證,因爲它在更新屬性後運行save方法。如果查看update_attribute的來源,您可以看到這一點。 http://apidock.com/rails/ActiveRecord/Base/update_attributes – stephenheron

2

您可以在控制器代碼中使用flash [:message]或flash [:notice]來存儲錯誤消息,該消息可用於顯示錯誤。 Link查看鏈接,清楚地說明了如何附加錯誤消息並將其用於顯示。實例變量doestnot包含任何錯誤,因爲在update中沒有驗證運行。

您可以使用@camera_errors = @ camera.save收集錯誤,然後

<% @camera_errors.errors.full_messages.each do |error| %> 
    <p><%= error %></p> 
    <% end %> 
6

剛擡起頭,你會得到一個錯誤Validation failed (ActiveRecord::RecordInvalid)用空的錯誤信息(如果沒有其他錯誤),當你有before_validation聲明和任何人返回false

注意before_validation回調不能返回falsenil是好的),而這可以通過意外,如發生,如果您分配false在該回調方法裏面的最後一行的布爾屬性。在你的回調方法中明確地寫出return true來完成這個工作(或者如果你的回調是一個塊,那麼最後真的如此)(如here)。

UPDATE:這將不再是首發的Rails 5.0的問題,因爲return false將不再停止回調鏈(throw :abort現在將停止回調鏈)。

UPDATE:您也可能會收到ActiveRecord::RecordNotSaved: Failed to save the record如果回調返回false

+0

哇。我從來不會猜到這一點。感謝節省我大概幾個小時的檢查! –

+1

@勞倫斯吳,是的,花了好幾個小時才找到自己的問題。 :-) –