【发布时间】:2011-06-22 13:48:21
【问题描述】:
在我的表单中
<%= label_tag("file", "Attachment:") %><%= file_field_tag "uploadfile" %>
在我的模型中我想写这个
validate :validates_uploadfile
def validates_uploadfile(file)
max_size = 2048
errors.add(:uploadfile, "File size exceeds limitation") if file.size > max_size
end
在我的控制器中我可以这样调用吗
validates_upload_file(params[:uploadfile])
有没有办法在上传之前验证文件上传(不是通过使用 javascript 或查看文件扩展名)
感谢您的帮助
UPD
validate :uploadfile_validation, :if => "uploadfile?"
def uploadfile_validation
errors[:uploadfile] << "should be less than 1MB" if uploadfile.size > 1.megabytes
end
【问题讨论】:
标签: ruby-on-rails-3 validation activerecord