たとえば、メーラーsend_signup_mail
が重く、完了するまでに時間がかかる場合sign_in_and_redirect
、ビューがレンダリングされた後に、最初に実行してバックグラウンドでメールを送信するにはどうすればよいですか。
以下のコードでは、直線的な順序で実行する必要がありますが、メールが送信されるまでリダイレクトが完了しないため、実際にはそうではないと思います。
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def facebook
# You need to implement the method below in your model (e.g. app/models/user.rb)
@fb_response = request.env["omniauth.auth"]
@user = User.from_omniauth(@fb_response)
if @user.persisted?
# Update user token
@user.fb_token = @fb_response.credentials.token
# Sign in
sign_in_and_redirect @user, :event => :authentication #this will throw if @user is not activated
set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format?
# Deliver signup mail
UserNotifier.send_signup_email(@user).deliver
else
session["devise.facebook_data"] = request.env["omniauth.auth"]
redirect_to new_user_registration_url
end
end
end
ありがとう!