SkyKOG
SkyKOG

Reputation: 287

Devise asks for confirmation before accepting invitation

I am creating a module in my rails application for "adding team members" to a project. And im using devise_invitable.

In this if i add a new email address both the confirmation mail and invitaion mail is being sent ... as that makes more sense

I just want the invitation mail to be sent (and after the user accepts the invitation mail he can be sent to the registration page) ... it does not make sense to confirm before accepting the invite.

My current code is as follows :

    def create_team_members
      # find the case
      @case = Case.find_by_id(params[:id])
      # TODO:: Will change this when add new cse functionality is done

      params[:invitation][:email].split(',').map(&:strip).each do |email|
      # create an invitation
      @invitation = Invitation.new(email: "#{email}".gsub(/\s+/, ''), role: params[:invitation][:role].rstrip, case_id: @case.id, user_type_id: params[:invitation][:user_type_id])

        if @invitation.save
          # For existing users fire the mail from user mailer
          if User.find_by_email(email).present?
            UserMailer.invite_member_instruction(@invitation).deliver
            @invitation.update_attributes(invited_by: current_user.id)

          else
            # For new users use devise invitable
            user = User.invite!(email: "#{email}", name: "#{email}".split('@').first.lstrip)
            user.skip_confirmation!
            user.save
            @invitation.update_attributes(invitation_token: user.invitation_token, invited_by: current_user.id)
          end
        end
      end
      redirect_to dashboard_path
    end

I dont know what im doing wrong ...

Please help ...thanks.

Upvotes: 0

Views: 934

Answers (1)

Aditya Kamatar
Aditya Kamatar

Reputation: 363

There is a work around, we can over ride the devise_invitable in the User Model. Currently your user model looks some what like this

class User < ActiveRecord::Base
   devise :confirmable, :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :invitable
   # Rest of the code
end

The method send_confirmation_instructions of devise is responsible of sending the confirmation mail. by overriding it we can stop the confirmation Email being sent and make the user confirmed at the same moment.

Change it to this

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :invitable

  # Overriding devise send_confirmation_instructions
  def send_confirmation_instructions
    super
    self.confirmation_token = nil    # clear's the confirmation_token
    self.confirmed_at = Time.now.utc # confirm's the user
    self.save
  end

  # Rest of the code
end

Now the Mail will not be sent.

Upvotes: 3

Related Questions