【发布时间】:2011-09-06 16:14:49
【问题描述】:
我的模型中有许多标准导轨验证:
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 版本 ruby 1.9.2p290 和 Rails 版本 3.1.0。
任何帮助都会很棒!
谢谢
【问题讨论】:
-
控制器代码在这里可能会有所帮助。
-
谢谢,我已经添加了控制器更新操作。
-
我设法找到了问题的根源,但由于我在另外 7 个小时内无法回答自己的问题,所以我暂时将解决方案放在这里。在我使用的控制器中:redirect_to :action => :edit 我应该一直在使用:render :action => :edit 通过使用 redirect_to 我在控制器中点击编辑操作,然后从数据库中获取一个新的相机对象而不是从更新操作中保留相机对象。
标签: ruby-on-rails ruby-on-rails-3.1