我有我的模型內的一些標準軌驗證:導軌不產生驗證失敗的消息
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版本。
任何援助將是偉大的!
感謝
控制器代碼在這裏可能會有所幫助。 – jdl
謝謝,我添加了控制器更新操作。 – stephenheron
我設法解決了我的問題,但由於我無法回答自己的問題7個小時,我暫時將解決方案放在這裏。在控制器我使用: redirect_to的:行動=>:編輯 我應該已經使用: 渲染:行動=>:編輯 通過使用redirect_to的我被擊中,然後將其在控制器內的編輯操作從數據庫中獲取新的相機對象,而不是從更新操作保留相機對象。 – stephenheron