ランダム トークンをデータベースに保存する必要があります。メールアドレスに送信しているので、登録時に User モデルに追加できるように、おそらくメールも保存する必要があります。以下は、実行できる関連部分の一部です (ただし、ギャップを埋める必要があります)。
RegistrationToken.new(:email => "their email address")
基本的に、コントローラでを使用して登録トークンを生成する必要があります。
このモデルは、ランダム トークンの生成を実装します。
class RegistrationToken < ActiveRecord::Base
# the secret is just to make the random number generator more secure
@@secret = 6345
def initialize
# info at http://www.ruby-doc.org/core-1.9.3/Random.html
seed = Random.new().integer(1000000000) + @@secret
token = Random.new(seed).integer(1000000000);
# you might want to generate random numbers larger than 1 billion
# for more security (maybe with 64 bit integers?)
end
end
データベースの移行:
class CreateRegistrationTokens
def change
create_table :products do |t|
t.string :email
t.integer :token
t.timestamps
end
end
end
次に、コントローラーとビューをセットアップするだけです。
登録コントローラーには、次のようなものが必要です。
class RegistrationsController < ActiveRecord::Base
def new
@registration_token = RegistrationToken.find(params[:token]).first
if @registration_token.nil?
raise 'Invalid token' # or whatever you want, eg. redirect to a page
end
# otherwise render the registration form (can do implicit render)
end
def create
@registration_token = RegistrationToken.find(params[:token]).first
if @registration_token.nil?
raise 'Invalid token' # or whatever you want, eg. redirect to a page
end
# otherwise create the user, eg.
User.create(params[:user].merge(:email => @registration_token.email))
@registration_token.destroy
end
end
上記のコントローラ アクションは基本的に、一致するトークンがデータベースで見つかった場合にのみ登録できるようにします。