2

ユーザーが最初にサイトから詳細情報を要求し、その後、実際の登録ページへのリンクを含む電子メールを受信する登録プロセスを設定したいと考えています。リンクはランダムに生成された URL である必要があり、登録ページへのアクセスは制限されている必要があります。つまり、ブラウザーに手動で URL を入力して登録ページにアクセスできないようにする必要があります。

これらの機能を実装する最善の方法についてアドバイスをいただければ幸いです。

私はRailsを初めて使用するので、この質問が基本的なものであるか、すでにカバーされている場合は、事前にお詫び申し上げます。

4

1 に答える 1

0

ランダム トークンをデータベースに保存する必要があります。メールアドレスに送信しているので、登録時に 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

上記のコントローラ アクションは基本的に、一致するトークンがデータベースで見つかった場合にのみ登録できるようにします。

于 2012-09-01T02:06:44.487 に答える