【发布时间】:2012-10-28 13:51:51
【问题描述】:
我试图通过上传文件(文件名、大小)来显示可能发生的错误。 我正在使用an adapted version of Jquery Fiel Upload for Ruby on Rails
我需要将我的错误消息(验证)关联到我的 json 响应中的“错误”键。
我想我必须更改 javascript 才能使其工作。
json:
render json: {error: @upload.errors.full_messages}, status: :unprocessable_entity
验证:
validates_uniqueness_of :upload_file_name, :message => "File with this name is already in the database"
validates :upload_file_size, :inclusion => {:in =>10.megabytes..20.megabytes}, :message =>"Too big or too small"
我用来上传文件的Javascript:
<script type="text/javascript" charset="utf-8">
$(function () {
// Initialize the jQuery File Upload widget:
$('#fileupload').fileupload();
//
// Load existing files:
$.getJSON($('#fileupload').prop('action'), function (files) {
var fu = $('#fileupload').data('fileupload'),
template;
fu._adjustMaxNumberOfFiles(-files.length);
console.log(files);
template = fu._renderDownload(files)
.appendTo($('#fileupload .files'));
// Force reflow:
fu._reflow = fu._transition && template.length &&
template[0].offsetWidth;
template.addClass('in');
$('#loading').remove();
});
});
</script>
我也尝试过这些事情:
json: => [{:error=> @upload.errors.full_messages }] }
使用它只显示验证消息,但我需要状态:以及
【问题讨论】:
标签: javascript ruby-on-rails json