You should already have a UserController or something like that for the registration purposes that you are currently accessing on the specified signup_url route. Suppose this route now looks something like this:
http:
All you have to do is check the prompt in the controller action and process it accordingly like this:
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
Without the invitation code, you are simply redirected to the root page. You might want DRY to check. When someone uses an invitation code, you can remove it from the database. I wrapped it in a transaction, but it is up to you (creating a user may be more important).
If you want to create a page that allows users to create invitations without registering, just do not add authentication to the InvitationsController and update this snippet:
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
I'm not sure if I covered all the bases, but I think this should point you in the right direction.
Jaryl source share