【问题标题】:rails, how to write a callback function after ajax request is finishedrails,ajax请求完成后如何编写回调函数
【发布时间】:2012-04-05 04:26:03
【问题描述】:

我想知道在 ajax 请求完成后如何/在哪里编写回调函数?

我正在构建一个 twitter 应用程序,并在用户单击“关注”后尝试发送电子邮件通知。以下/取消关注是使用 ajax 完成的。当用户点击关注时,我有...

  def follow!(other_user)
    relationships.create!(followed_id: other_user.id)
  end

依次调用

  def create
    @user = User.find(params[:relationship][:followed_id])
    current_user.follow!(@user)
    respond_to do |format|
        format.html {redirect_to @user}
        format.js
    end

    UserMailer.is_now_following(@user, current_user).deliver
  end

但是,通过添加该 UserMailer 行,它大大落后于我的 ajax。我应该在哪里/如何放置电子邮件请求?我在考虑 ajax 完成后的回调函数,但我不确定在哪里添加它或如何添加。

对不起,我对此还是很陌生。非常感谢!

嗯,我发现使用 jquery 可能是一个好方法。我决定给个关注按钮

<%= f.submit "Follow", :class => "btn btn-large btn-primary", 
        :id => "follow_button"%>

一个id和使用的jquery

$("#follow_button").bind('ajax:success', function() {

});

之后发送电子邮件。但是,我真的很确定我如何传递变量。我想要达到的最终路线是

UserMailer.is_now_following(@user, current_user).deliver

current_user 是一个函数 btw,它分配/返回 current_user

我该怎么做?谢谢!

【问题讨论】:

    标签: ruby-on-rails ajax callback


    【解决方案1】:
    【解决方案2】:
    猜你喜欢
    • 2015-12-28
    • 1970-01-01
    • 2016-07-22
    • 1970-01-01
    • 2015-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多