你如何处理铁路的闪光灯Ajax请求? [英] How do you handle Rail's flash with Ajax requests?
问题描述
我是pretty的满意解决方案的,我来了。基本上,我有一个重新加载闪存内嵌一个辅助方法,然后我有清除闪光灯,如果请求XHR的after_filter。有没有人有比这更简单的解决方案吗?
I'm pretty happy with the solution that I came up with. Basically, I have a helper method that reloads the flash inline, and then I have an after_filter that clear out the flash if the request is xhr. Does anyone have a simpler solution than that?
更新:上面写回的Rails 1.x和不再支持的解决方案
Update: The solution above was written back in Rails 1.x and is no longer supported.
推荐答案
您也可以使用after_filter块存储的提示信息中的响应头和使用JavaScript显示出来:
You can also store the flash messages in the response headers using a after_filter block and display them using javascript:
class ApplicationController < ActionController::Base
after_filter :flash_to_headers
def flash_to_headers
return unless request.xhr?
response.headers['X-Message'] = flash[:error] unless flash[:error].blank?
# repeat for other flash types...
flash.discard # don't want the flash to appear when you reload page
end
而在application.js中添加全局AJAX的处理程序。 jQuery的做这样的事情:
And in application.js add a global ajax handler. For jquery do something like this:
$(document).ajaxError(function(event, request) {
var msg = request.getResponseHeader('X-Message');
if (msg) alert(msg);
});
替换警报()用自己的JavaScript闪光功能或尝试jGrowl。
Replace alert() with your own javascript flash function or try jGrowl.
这篇关于你如何处理铁路的闪光灯Ajax请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!