Rails User Email Acknowledgment

I am trying to create an email program that sends an email whenever a user signs up. Pretty simple, but I'm new to rails.

I have a site that is already creating a user. I have a login and registration page that works correctly, but you need help creating an email program that sends a confirmation link by email and maybe the ability to send these letters without registering a user, for example, creating a separate page for user invitations.

I created a model invitation .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 

and my invitiation_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 

What else do I need to edit? how can i connect this to an existing user account and inputs that are working fine?

+4
source share
2 answers

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://localhost:3000/register/code_here 

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.

+1
source

I don’t see where Mailer.deliver_invitation comes from, are you using a gem? would help if you created mailer.rb, do you have any mgs / stack trace error?

Check out a few guides here, 5 Configuring Mail Mailer . http://guides.rubyonrails.org/action_mailer_basics.html

Consider using the device for user authentication, https://github.com/plataformatec/devise It is complex, but well-documented, and easy to configure to run.

I assume that you are using Rails 3.1 (also works in earlier versions, just find the right guide for your version of Rails).

+1
source

Source: https://habr.com/ru/post/1385936/


All Articles