Mateusz Urbański
Mateusz Urbański

Reputation: 7862

Activiation via Email, Ruby on Rails 4

In my application that I working on I have a model: "Reservation". It has many different fields but this is informations are not important for my question. When user create reservation I want to have reservation activation via email. So:

What will be the best way to build this feature? Thank's in advance.

Upvotes: 0

Views: 40

Answers (1)

Abs
Abs

Reputation: 3962

You can use this by decoupling the confirmation token logic in a Rails Concern and reuse it elsewhere maybe login plus have a central confirmation token generation code.

module Tokenable
  extend ActiveSupport::Concern

  included do
    before_create :generate_token
  end

  protected

  def generate_token
    self.confirm_token = loop do
      random_token = SecureRandom.urlsafe_base64(15).tr('lIO0', 'sxyz')
      break random_token unless self.class.exists?(confirm_token: random_token)
    end
  end
end


class Reservation::ActiveRecord::Base
include Tokenable
end

In your Reservation Model Send Notification Email in a after_create or similar

Rest is Simple click link remove the token everything is active.

Upvotes: 3

Related Questions