【发布时间】:2013-09-07 09:25:25
【问题描述】:
我正在使用这个库 (https://github.com/CodeSeven/toastr),我正在尝试将我的 Flash 通知推送到 Toastr 为我提供的 javascript 函数。如何为每个错误或通知调用此函数?
这是用于制作烤面包机通知的方法之一:
toastr.warning('This is a warning!')
我尝试在 ApplicationController 中创建一个方法,以查看是否可以针对 CanCan 的默认错误调用该方法。我有各种版本的方法,但都不起作用。
def toast(type, text)
#if Logic here for various errors/notifications
respond_to do |format|
format.js { render action: "toastr.warning(#{text})", layout: false}
end
end
def toast(type, text)
#if Logic here for various errors/notifications
"toastr.warning(#{text})"
end
然后我尝试在 CanCan 块中使用此方法:
rescue_from CanCan::AccessDenied do |exception|
toast :error, exception.message
redirect_to root_url
end
我认为这是可能的,但我只是不确定如何实现它。没有多少人尝试这样做,这可能是有原因的。我愿意接受有关如何做我想做的事情的任何建议。
这是一个实现 Toast 通知的测试应用程序: http://codeseven.github.io/toastr/demo.html
【问题讨论】:
标签: javascript ruby-on-rails notifications ruby-on-rails-4 toastr