Javascript Rails在完成后通过Ajax发出Flash通知的方法
当我的控制器成功完成上传文件时,我希望通过ajax将flash通知发送回我的视图。但是,我不知道如何确切地处理来自服务器的响应。下面我所拥有的是我认为非常接近我想要的东西,但它不太起作用。任何帮助都将不胜感激。谢谢大家! 我的控制器Javascript Rails在完成后通过Ajax发出Flash通知的方法,javascript,jquery,ruby-on-rails,ajax,flash,Javascript,Jquery,Ruby On Rails,Ajax,Flash,当我的控制器成功完成上传文件时,我希望通过ajax将flash通知发送回我的视图。但是,我不知道如何确切地处理来自服务器的响应。下面我所拥有的是我认为非常接近我想要的东西,但它不太起作用。任何帮助都将不胜感激。谢谢大家! 我的控制器 def import begin Thread.new do Order.import(params[:file]) ActiveRecord::Base.connection.close end
def import
begin
Thread.new do
Order.import(params[:file])
ActiveRecord::Base.connection.close
end
rescue
redirect_to root_url, notice: "Invalid CSV file format."
end
format.js { flash.now[:notice] = "Here is my flash notice" }
end
我的Ajax Javascript
$(function() {
var bar = $('.bar');
var percent = $('.percent');
var status = $('#status');
$('form').ajaxForm({
beforeSend: function() {
status.empty();
var percentVal = '0%';
bar.width(percentVal);
percent.html(percentVal);
},
uploadProgress: function(event, position, total, percentComplete) {
var percentVal = percentComplete + '%';
bar.width(percentVal);
percent.html(percentVal);
},
complete: function(xhr) {
status.html(xhr); //I think I need to change this but not sure what to
}
});
});
我收到一个内部服务器错误500。OrdersController中的ArgumentError在您的代码和我的代码中导入的参数太少。如果我重定向,它会在网页中给我一个网页副本,否则会导致内部服务器错误。你知道为什么吗?
def import
begin
Thread.new do
Order.import(params[:file])
ActiveRecord::Base.connection.close
end
rescue
redirect_to root_url, notice: "Invalid CSV file format."
end
@notice = "Here is my flash notice"
end
$(function() {
var bar = $('.bar');
var percent = $('.percent');
var status = $('#status');
$('form').ajaxForm({
beforeSend: function() {
status.empty();
var percentVal = '0%';
bar.width(percentVal);
percent.html(percentVal);
},
uploadProgress: function(event, position, total, percentComplete) {
var percentVal = percentComplete + '%';
bar.width(percentVal);
percent.html(percentVal);
},
complete: function(response) {
// replace the response in the div which displays notice
status.html(response)
}
});
});