0

Whenever a model is created in my application, an email is sent out to several people. The response to the model creation is JS, so there is a slight delay in updating the page because first all the emails are being sent then the JS view logic kicks in (i.e. there is a noticeable lag between the time the Create button is pushed and when the page updates itself).

Is there a way I can set up my controller so that the emails are sent AFTER the view is finished updating?

Here is what my Controller action basically looks like:

def create
  @model = Model.new(params[:model])
  @model.save

  @followers.each do |f|
    ModelMailer.some_email(f).deliver
  end

  respond_to do |format|
    format.js { render :layout => false }
  end
end
4

2 に答える 2

2

DelayJob または他のバックグラウンド ワーカーを使用してこれを行い、handle_asynchronously :deliverメーラーに追加できます。

于 2012-06-18T07:07:44.263 に答える
2

メールの送信を処理する正しい方法であるため、Hauleth の回答に賛成しました。

もう 1 つのオプションは、システムの動作中にユーザーにフィードバックを提供することです。「読み込み」や「処理」など、さまざまな名前で呼ばれる UI デザイン パターンです。つまり、リクエストを送信する前にクライアントにメッセージを設定し、応答が完了したらそのメッセージを成功または失敗で更新します。応答自体はタイムアウトになる可能性があるため、メッセージは一時的なものです。

アクションがリモート (別名 ajax) の場合は、常にこの UI パターンを適用する必要があります。

アクションがまったく新しいページをレンダリングしている場合、あなたの場合のように、応答が遅くなる可能性が高いことがわかっている場合に役立ちます。

于 2012-06-18T15:00:20.137 に答える