使用respond_with 如何使用ajax 响应闪烁通知?

发布于 2025-01-07 03:30:28 字数 67 浏览 1 评论 0原文

flash 方法仅在页面重新加载后显示消息...当我使用 :remote => 时,我该怎么做才能让它出现?真的?

The flash method only shows the message after the page reloads... what do I do to get it to appear when I'm using :remote => true?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怪我入戏太深 2025-01-14 03:30:28

如果您正在使用 :remote => true 你实际上是在做 Ajax 请求。
在这种情况下,闪光灯将无法工作。

您需要的是一个模仿该闪现消息的 js 函数。

我通常会这样:(Mootools,但你可能明白)

showMessage: function(message, style, addReload)
    {
        var class_name = style + "_message message";

        var flash = $("flash_message");

        if (flash) {
            flash.dispose();
        }

        flashElement = new Element("div", {
            id: 'flash_message'
        });
        flashElement.set('class', class_name);

        var strong = new Element('strong', { 
                                html: message
                                });

        if(addReload)
        {
            strong.adopt(
                            new Element("a", {href: window.location, html: 'Reload'})
                        );
        }

        flashElement.adopt(
                            strong
                        );


        flashElement.inject($("mainPageContainer"));
        Site.show_message();
    },

每当我使用 :remote =>是的,我在 js 视图中有这个

<% flash.discard %>
Dashboard.showMessage('Comment added and was sent to clients', 'notice');

if you are using :remote => true you are actually doing Ajax request.
in that case, the flash will not work.

What you need is to have a js function that mimics that flash message.

I usually have that: (Mootools, but you probably get the idea)

showMessage: function(message, style, addReload)
    {
        var class_name = style + "_message message";

        var flash = $("flash_message");

        if (flash) {
            flash.dispose();
        }

        flashElement = new Element("div", {
            id: 'flash_message'
        });
        flashElement.set('class', class_name);

        var strong = new Element('strong', { 
                                html: message
                                });

        if(addReload)
        {
            strong.adopt(
                            new Element("a", {href: window.location, html: 'Reload'})
                        );
        }

        flashElement.adopt(
                            strong
                        );


        flashElement.inject($("mainPageContainer"));
        Site.show_message();
    },

Whenever I use :remote => true, I have that in the js view

<% flash.discard %>
Dashboard.showMessage('Comment added and was sent to clients', 'notice');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文