Rails 4 - Toaster通知而不是Flash通知 [英] Rails 4 - Toaster notifications rather than flash notifications

查看:170
本文介绍了Rails 4 - Toaster通知而不是Flash通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此库,( https://github.com/CodeSeven/toastr )我正在尝试将我的Flash通知推送到Toastr为我提供的javascript函数。如何针对每个错误或通知调用此函数?

I am using this library, (https://github.com/CodeSeven/toastr) and I am trying to push my Flash notifications to the javascript function Toastr has provided for me. How do I call this function for every error or notification?

这是用于制作烤面包机通知的方法之一:

This is one of the methods that are used for making a toaster notification:

toastr.warning('This is a warning!')

我尝试在ApplicationController中创建一个方法,看看我是否可以在CanCan的默认错误上调用该方法。我有各种版本的方法,没有一个工作。

I tried making a method in the ApplicationController to see if I could call that method on default errors from CanCan. I have various versions of the method, none of which worked.

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块中使用此方法:

And then I try to use this method in the CanCan block:

rescue_from CanCan::AccessDenied do |exception|
    toast :error, exception.message
    redirect_to root_url
end

我认为这是可能的,但我只是不确定如何实现它。没有多少人尝试这样做,可能有一个原因。我对如何做我想做的任何建议持开放态度。

I would assume that this is possible, but I am just unsure how to implement it. Not many try to do this, and there is probably a reason. I am open to any suggestions on how to do what I am trying to do.

这是一个实现Toast通知的测试应用程序:
http://codeseven.github.io/toastr/demo.html

Here is a testing application that implements the Toast notifications: http://codeseven.github.io/toastr/demo.html

推荐答案

我建议的是为这类东西制作一个新的 flash 类型然后在你的布局中将其渲染为JS。

What I'd recommend is to make a new flash type for this sort of thing and then render that as JS in your layout.

ApplicationController

def toast(type, text)
  flash[:toastr] = { type => text }
end


app/views/layouts/<your layout>.html.erb
# (or in a partial to be reused in various layouts)
# the <script> tag isn't needed if code snippet is
# included in an existing script block, of course.

<% if flash[:toastr] %>
  <script type="text/javascript">
    <% flash[:toastr].each do |type, message| %>
      toastr.<%= type %>(<%= message.inspect %>)
    <% end %>
  </script>
<% end %>

这样你就可以从 flash 对象,您可以直接通过erb轻松理解在您的视图中编写的javascript。您可能需要向 ApplicationController#toast 方法添加选项哈希,这样您就可以执行 flash.now [:toastr] 有时,当然。等等......但这应该让你开始。

So this way you get all the standard behavior you're used to from the flash object and you get easy to understand javascript written in your views directly via erb. You may need to add an options hash to the ApplicationController#toast method so you can do a flash.now[:toastr] at times, of course. And so on... But this should get you started.

这篇关于Rails 4 - Toaster通知而不是Flash通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆