akvarel
10-28-2012, 08:53 PM
I am trying to show the errors that can occur by uploading a file (file name, size).
I am using an adapted version of Jquery Fiel Upload for Ruby on Rails (https://github.com/tors/jquery-fileupload-rails-paperclip-example)
I need to have my error messages (validations) associated to the 'error' key in my json response.
I guess I have to change the javascript to make it work.
json:
render json: {error: @upload.errors.full_messages}, status: :unprocessable_entity
validations:
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 I use to upload the files:
<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>
I have tried as well those things:
`json: => [{:error=> @upload.errors.full_messages }] }`
using this shows only the validation message but I need the status: as well
I was suggested as well to do this way. But I dont have a js.file, the only javascript I have is in the index.html.erb and you can see it above.
render :json => { :errors => @example.errors.full_messages }, :status => 422
in your js file:
$('.form_id_or_class').live('submit', function() {
$.ajax({
url: $(this).attr("action"),
type: "POST",
data: $(this).serialize(),
success: function(data) {
// do some action
}
,error: function(xhr){
var errors = $.parseJSON(xhr.responseText).errors
}
});
return false;
});
Many thanks in advanced
I am using an adapted version of Jquery Fiel Upload for Ruby on Rails (https://github.com/tors/jquery-fileupload-rails-paperclip-example)
I need to have my error messages (validations) associated to the 'error' key in my json response.
I guess I have to change the javascript to make it work.
json:
render json: {error: @upload.errors.full_messages}, status: :unprocessable_entity
validations:
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 I use to upload the files:
<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>
I have tried as well those things:
`json: => [{:error=> @upload.errors.full_messages }] }`
using this shows only the validation message but I need the status: as well
I was suggested as well to do this way. But I dont have a js.file, the only javascript I have is in the index.html.erb and you can see it above.
render :json => { :errors => @example.errors.full_messages }, :status => 422
in your js file:
$('.form_id_or_class').live('submit', function() {
$.ajax({
url: $(this).attr("action"),
type: "POST",
data: $(this).serialize(),
success: function(data) {
// do some action
}
,error: function(xhr){
var errors = $.parseJSON(xhr.responseText).errors
}
});
return false;
});
Many thanks in advanced