我试图通过上传文件(文件名,大小)来显示可能发生的错误。 我正在使用ad adapted version of Jquery Fiel Upload for Ruby on Rails 我试图修改这个代码截止但我不能。
else
format.html { render action: "new" }
format.json { render json: @upload.errors, status: :unprocessable_entity }
end
我有以下验证,我希望json在发生错误时写,所以像这样:
else
format.html { render action: "new" }
format.json { render json: @upload.errors, status: :unprocessable_entity, error:"Error!" }
end
(它做的工作)我需要一个错误的规范,这是在验证:消息中写的
我的验证:
validates :upload_file_name, :presence => true,
:format =>{:with => %r{\.(cel)$}i}
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"
def to_jq_upload
{
"name" => read_attribute(:upload_file_name),
"size" => read_attribute(:upload_file_size),
"url" => upload.url(:original),
"delete_url" => upload_path(self),
"delete_type" => "DELETE"
}
end
end
和我唯一的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
答案 0 :(得分:0)
我不确定我是否理解您的问题,但如果您需要在json响应中将'error'
键与错误消息相关联,则可以执行以下操作:
render json: {error: @json.errors.full_messages}, status: :unprocessable_entity