4

ユーザーがサインアップするたびに電子メールを送信するメーラーを作成しようとしています。かなり単純ですが、私はレールが初めてです。

既にユーザーを作成しているサイトがあります。正しく機能するログインおよびサインアップ ページがありますが、電子メール確認リンクを送信するメーラーの作成と、ユーザーの招待用に別のページを作成するなど、ユーザーがサインアップせずにこれらの電子メールを送信するオプションを作成するのに助けが必要です。

モデルの招待状を作成しました.rb

  class Invitation < ActiveRecord::Base
  belongs_to :sender, :class_name => 'User'
  has_one :recipient, :class_name => 'User'

  validates_presence_of :recipient_email
  validate :recipient_is_not_registered
  validate :sender_has_invitations, :if => :sender

  before_create :generate_token
  before_create :decrement_sender_count, :if => :sender

  private

  def recipient_is_not_registered
    errors.add :recipient_email, 'is already registered' if User.find_by_email(recipient_email)
  end

  def sender_has_invitations
    unless sender.invitation_limit > 0
      errors.add_to_base 'You have reached your limit of invitations to send.'
    end
  end

  def generate_token
    self.token = Digest::SHA1.hexdigest([Time.now, rand].join)
  end

  def decrement_sender_count
    sender.decrement! :invitation_limit
  end 
  #attr_accessible :sender_id, :recipient_email, :token, :sent_at
end

そして私のinvitation_controller.rb

class InvitationsController < ApplicationController
  def new
    @invitation = Invitation.new
  end

  def create
    @invitation = Invitation.new(params[:invitation])
    @invitation.sender = current_user
    if @invitation.save
      if logged_in?
        Mailer.deliver_invitation(@invitation, signup_url(@invitation.token))
        flash[:notice] = "Thank you, invitation sent."
        redirect_to projects_url
      else
        flash[:notice] = "Thank you, we will notify when we are ready."
        redirect_to root_url
      end
    else
      render :action => 'new'
    end
  end
end

他に何を編集する必要がありますか? これを、正常に機能している既存のユーザーサインアップとログインに接続するにはどうすればよいですか?

4

2 に答える 2

2

現在、routeという名前のsignup_urlを介してアクセスしている、登録用のUsersControllerなどがすでにあるはずです。このルートが次のようになったとします。

http://localhost:3000/register/code_here

ここで行う必要があるのは、コントローラーアクションで招待を確認し、それに応じて次のように処理することです。

def new
  invite = Invite.find_by_token(params[:id]

  if invite.nil?
    redirect_to root_path, :notice => "Sorry, you need an invite to register"
  end

  @user = User.new(:email => invite.recipient_email)

end

def create
  invite = Invite.find_by_token(params[:token]

  if invite.nil?
    redirect_to root_path, :notice => "Sorry, you need an invite to register"
  end

  begin

    invite.nil.transaction do
      invite.nil.destroy!
      @user = User.create(params[:user)
    end

    redirect_to my_dashboard_path, :notice => "Yay!"

  rescue ActiveRecord::RecordInvalid => invalid
     render :new, :alert => "Validation errors"
  end

end

招待コードがなければ、ルートページにリダイレクトするだけです。ただし、そのチェックを乾燥させたい場合があります。誰かが招待コードを使用するとき、あなたはそれをデータベースから削除したいかもしれません。トランザクションでまとめましたが、これはあなた次第です(ユーザーの作成がより重要な場合があります)。

ユーザーがサインアップせずに招待状を作成できるページを作成する場合は、InvitationsControllerに認証を追加せず、次のスニペットを更新してください。

def create

  @invitation = Invitation.new(params[:invitation])
  @invitation.sender = current_user if logged_in?

  if @invitation.save

    Mailer.deliver_invitation(@invitation, signup_url(@invitation.token))
    flash[:notice] = "Thank you, invitation sent."

    if logged_in?
      redirect_to projects_url
    else
      redirect_to root_url
    end

  else
    render :action => 'new'
  end

end

すべての拠点をカバーしたかどうかはわかりませんが、少なくとも正しい方向を示す必要があると思います。

于 2011-12-12T17:32:33.640 に答える
1

Mailer.deliver_invitation がどこから来たのかわかりません。gem を使用していますか? mailer.rb を作成していただけると助かりますが、エラー mgs/ スタック トレースはありますか?

いくつかのガイドがあるこちらをご覧ください。 5 Action Mailer Configuration http://guides.rubyonrails.org/action_mailer_basics.html

ユーザー認証 にデバイスを使用することを検討してください。

Rails 3.1を使用していると仮定します(以前のバージョンでも機能します。Railsバージョンの正しいガイドを見つけてください)

于 2011-12-12T17:26:03.793 に答える