Flash消息未在模式关闭中显示 [英] Flash message not showing on modal close

查看:91
本文介绍了Flash消息未在模式关闭中显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我有一个模态,用户可以在其中输入电子邮件地址来邀请人们,并且在单击按钮时,它将发出ajax请求以发送电子邮件.发送电子邮件后,它应该闪烁一条成功消息,但是即使响应显示已成功,也不会闪烁.

So I have a modal where users can put in email addresses to invite people and upon clicking the button, it makes an ajax request to send emails. After the emails are sent, it should flash a success message however it doesn't even though the response says it was successful.

我的Ajax代码:

$.ajax({ 
  url: '/send_invite_email',
  type: 'POST',
  data: {"emails": $(".email-input").val()},
  success: function() {
    $('#invite-modal').modal('hide');
  }
});

InvitesController:

InvitesController:

def send_invite_email
   @emails = params[:emails]
   @invited_by = current_user.id
   for email in @emails.split(/[\s,]+/)
     InvitationMailer.invite(email, @invited_by).deliver_later
     next if Invitation.find_by_invited_by_and_invitee(@invited_by, email)
     Invitation.create! invited_by: @invited_by, invitee: email
   end
   respond_to do |format|
     format.html { 
        flash[:success] = 'Invitation email sent!'
        redirect_to about_path
     }
     format.js
   end
end

推荐答案

您对响应不做任何事情.首先,您需要在成功处理程序中添加一个参数,以便能够访问数据,然后在处理程序中对数据进行某些操作

You aren't doing anything with the response. First you need to add an argument to the success handler to be able to access the data and then within the handler do something with the data

$.ajax({ 
  url: '/send_invite_email',
  type: 'POST',
  data: {"emails": $(".email-input").val()},
  success: function(serverResponse) {
    // do something with response
    alert( serverResponse);

    $('#invite-modal').modal('hide');
  }
});

参考 $ .ajax文档

这篇关于Flash消息未在模式关闭中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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