简体   繁体   中英

Session in Action Mailer - how to pass it?

Let's say I have a website where people can get a free ebook if they will sign up for a newsletter - after they've done it, I will create a User model and I will show them Edit Form to add some extra details about them.

I don't want to force them to add a password or any other details on the first page because it would decrease conversions and I don't require the additional information either. Also, I don't want them to have forever access to the Edit page so I solved it by assigned a session to them and recognize it through it on the Edit page. This is my controller:

class UsersController < ApplicationController

  def create
    user = User.new(user_params)
    if user.save
      session[:user_id] = user.id
      UserWorker.perform_in(5.minutes, 'new_user', user.id)
      redirect to edit form...
    end
  end

  def edit
    @user = User.find(session[:user_id])
  end

  def update
    @user = User.find(session[:user_id])
    @user.update!(user_edit_params)
    redirect_to user_thank_you_path
  end
end

But if they won't add extra information within 10 mins, I will send them an email via ActiveMailer with a link to the Edit form and ask them to do so.

Th question is how could I identify the user through the session and show them the form - how could I do User.find(session[:user_id] via ActionMailer)? Is it actually a correct way or would you recommend a different approach?

One way could be to set a background job to run in 10 minutes.

Inside that job, you would check if they're still "unregistered". You deliver the email if they've yet to complete the registration.

Something like this;

class UsersController < ApplicationController
  def create
    user = User.new(user_params)
    if user.save
      session[:user_id] = user.id
      RegistrationCompletionReminderWorker.perform_in(10.minutes, user.id)

      # redirect to edit form...
    end
  end
end


class RegistrationCompletionReminderWorker
  def perform(user_id)
    user = User.find(user_id)

    if user.password.nil? # or whatever your logic for registration completion is
      UserMailer.registration_reminder(user_id).deliver_now
    end
  end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM