Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 带有ajax和jquery noty插件的Rails flash消息_Javascript_Jquery_Ruby On Rails - Fatal编程技术网

Javascript 带有ajax和jquery noty插件的Rails flash消息

Javascript 带有ajax和jquery noty插件的Rails flash消息,javascript,jquery,ruby-on-rails,Javascript,Jquery,Ruby On Rails,我用ajax对profile进行了评论。当有人添加评论页面无法重新加载时,我会对flash消息产生问题 我的评论如下所示: @comment = @profile.comments.includes(:user).new(params[:comment]) if @comment.save flash[:add_comment_success] = "Comment added" respond_to do |format| format.html {redirect_to :

我用ajax对profile进行了评论。当有人添加评论页面无法重新加载时,我会对flash消息产生问题

我的评论如下所示:

@comment = @profile.comments.includes(:user).new(params[:comment])
if @comment.save
  flash[:add_comment_success] = "Comment added"
  respond_to do |format|

    format.html {redirect_to :back}
    format.js
  end

else
var flashError = "<%=flash[:add_comment_success]%>";
if (flashError){
  $("#flash_messages").html("<div class='messages_notify'><div id='message-add_comment_success'><p class='message_content'>" +flashError +  "</p></div></div>");
}
$(document).on("ready page:change", function() {
  noty_load();
});

function noty_load(){

$( ".messages_notify #message-add_comment_success" ).each( function(){
    generate('success', 'comment added');
});
我的create.js.erb用于创建带有flash消息的评论,如下所示:

@comment = @profile.comments.includes(:user).new(params[:comment])
if @comment.save
  flash[:add_comment_success] = "Comment added"
  respond_to do |format|

    format.html {redirect_to :back}
    format.js
  end

else
var flashError = "<%=flash[:add_comment_success]%>";
if (flashError){
  $("#flash_messages").html("<div class='messages_notify'><div id='message-add_comment_success'><p class='message_content'>" +flashError +  "</p></div></div>");
}
$(document).on("ready page:change", function() {
  noty_load();
});

function noty_load(){

$( ".messages_notify #message-add_comment_success" ).each( function(){
    generate('success', 'comment added');
});
}

我认为ajax加载有一个问题。我必须以某种方式在app.js中检查是否加载了ajax,然后运行noty_load()。当我签入firebug时,我看到了flash通知,但在没有重新加载页面的情况下,我不会看到它。当发出ajax请求时,如何运行noty_load?

在create.js.erb的末尾调用
noty_load()
。如果您想使用干燥剂方法,您可以对所有js响应使用如下布局:

@comment = @profile.comments.includes(:user).new(params[:comment])
if @comment.save
  flash[:add_comment_success] = "Comment added"
  respond_to do |format|

    format.html {redirect_to :back}
    format.js
  end

else
var flashError = "<%=flash[:add_comment_success]%>";
if (flashError){
  $("#flash_messages").html("<div class='messages_notify'><div id='message-add_comment_success'><p class='message_content'>" +flashError +  "</p></div></div>");
}
$(document).on("ready page:change", function() {
  noty_load();
});

function noty_load(){

$( ".messages_notify #message-add_comment_success" ).each( function(){
    generate('success', 'comment added');
});
application.js.erb:

<%= yield %>;
<% if flash.present? %>
  <% flash_message = flash.first %>
  $("#flash_messages").html("<div class='messages_notify'><div id='message-<%= j(flash_message.first) %>'><p class='message_content'><%= j(flash_message.last) %></p></div></div>");
  noty_load();
<% end %>
;
$(“#flash_messages”).html(“

”; noty_load();
正常,但如何删除通知?在页面上,当我添加注释时,只有一个通知,但当我重新加载它或去某个地方时,它也会生成下一个3 noty$(文档);当我这样做时,它只显示了一个额外的诺蒂(不是3个,而是一个额外的诺蒂)如何删除额外的诺蒂all@Michael检查我编辑的答案。我在显示函数的末尾添加了
.remove()
,以便在消息显示后立即删除消息。您不需要解除绑定。它的工作原理类似于解除绑定-在重新加载页面时,我还有一个额外的noty。@Michael尝试将
事件作为参数添加到事件处理程序中,并在
noty_load
之前添加以下行:
控制台.log(event.type)
查看事件是如何触发的。