Rails 通过 ajax 发出通知 [英] Rails flash notice via ajax

查看:34
本文介绍了Rails 通过 ajax 发出通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

长话短说,我有一个按钮.单击它时,我希望触发一个 ajax 请求,该请求获取 flash[:notice] 并将其显示在 $

中的 div 中

这是我的简短观点:

 <div id="通知">

我在视图中的ajax请求:

$("#search").submit(function(){$.ajax({类型:POST",url://url 到我的显示操作成功:功能(数据){/*$("#notice").html("<%= flash[:notice] %>");$("#content").html(数据);*/}});返回假;});

我的控制器:

def HomeController <动作控制器::基础定义索引结尾高清秀response_to do |格式|format.js { flash[:notice] = "" + count.to_s + " 结果为 " + params[:query][:search_key] + "" }结尾#render :partial =>'搜索'结尾结尾

我的 show.js.erb

#app/views/dashboard_home/show.js.erb$("#notice").html("<%=j flash[:notice] %>");$("#content").html("<%=j 渲染部分:"搜索"%>");

问题是当我点击按钮时,通知显示正常.但同样的通知在下次点击时仍然存在.搜索部分包含表格请帮忙!

解决方案

感谢 Rich Peck 的回答,这是我得到的一个例子.我需要使用 flash.now 来确保 flash 通知不会持续存在.

视图中的 AJAX 触发器:

<%= link_to "Email report", users_path, remote: true %>

控制器:

# app/controllers/users_controller类用户控制器 <应用控制器定义索引# 在这里做一些事情response_to do |格式|format.js { flash.now[:notice] = "这是我的闪光通知" }结尾结尾结尾

渲染视图:

# app/views/users/index.js.erb$("#flash").html('<%= j 渲染部分: "shared/notice_banner" %>');

Flash 通知在布局中的显示位置:

# app/views/layouts/application.html.erb<div id="闪存"><% if notice.present?%><%=渲染部分:shared/notice_banner"%><%结束%>

# app/views/shared/_notice_banner.html.erb<div data-alert class="alert-box"><%=通知%><a href="#" class="close">&times;</a>

Long story short, I have a button. On clicking it, I want an ajax request to be triggered which gets flash[:notice] and displays it in a div in$

Here is my shortened view:

 <input type="button" id="search" value="display"/>

 <div id="notice">

 </div>

My ajax request in the view:

$("#search").submit(function(){
                            $.ajax({
                                type: "POST",
                                url: //url to my show action
                                success: function(data){
                                      /*$("#notice").html("<%= flash[:notice] %>");
                                        $("#content").html(data);*/
                                }
                            });
                            return false;
                    });

My controller:

def HomeController <  ActionController::Base
  def index

  end

  def show
    respond_to do |format|
    format.js {  flash[:notice] = "" + count.to_s + " results found for " + params[:query][:search_key] + "" }
    end
    #render :partial => 'search'
  end
end

My show.js.erb

#app/views/dashboard_home/show.js.erb
$("#notice").html("<%=j flash[:notice] %>");

$("#content").html("<%=j render partial: "search" %>");

The problem is when I click on button, the notice is displayed fine. But the same notice persists on the next clicks too. The search partial contains the table Please help!

解决方案

Here is an example that I got working, thanks to Rich Peck's answer. I needed to use flash.now to make sure the flash notice didn't persist.

AJAX trigger in the view:

<%= link_to "Email report", users_path, remote: true %>

Controller:

# app/controllers/users_controller
class UsersController < ApplicationController

  def index
    # do some things here

    respond_to do |format|
      format.js { flash.now[:notice] = "Here is my flash notice" }
    end
  end
end

Rendered view:

# app/views/users/index.js.erb
$("#flash").html('<%= j render partial: "shared/notice_banner" %>');

where the flash notice is displayed in the layout:

# app/views/layouts/application.html.erb
<div id="flash">
  <% if notice.present? %>
    <%= render partial: "shared/notice_banner" %>
  <% end %>
</div>


# app/views/shared/_notice_banner.html.erb
<div data-alert class="alert-box">
  <%= notice %>
  <a href="#" class="close">&times;</a>
</div>

这篇关于Rails 通过 ajax 发出通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
前端开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆