0

In my Rails/Devise app I need to manually send confirmation email for user with specific role and delay its sending.

What I do now is: a) In User.rb:

before_save :skip_mail_if_role1
def skip_mail_if_role1
  self.skip_confirmation_notification! if self.role == 'role1'
end

b) In custom signup controller for this role user:

Devise::Mailer.confirmation_instructions(resource, resource.confirmation_token).deliver_later(wait: 5.minutes)

Of course I get an email with wrong token and button click does nothing. Any dummy token also does nothing. When I pass no second argument I get arguments error (there should be 2 of them at least and this is werid as far as here they pass only user argument).

So I wonder what am I to pass there as a token from controller to make this work?

UPDATE:

Using this instruction I implemented custom devise MyMailer but I still have no idea what should I pass as token attribute in controller while calling MyMailer.confirmation_instructions(resource, ????).deliver

Community
  • 1
  • 1
alv_721
  • 305
  • 3
  • 18

1 Answers1

1

i overrided the default logic from devise to generate the token and then used it to confirm and send email...

in user.rb

  def create_password_reset_token
    Rails.logger.info "=======Creating reset password token======="
    generate_token(:reset_password_token) ##unless self.reset_password_token.present?
    update_attribute(:reset_password_sent_at,Time.zone.now)
    save!
    ####refresh the user to get latest token which is used to reset password as find_by_reset_password_token
    self.reload
  end

  def generate_token(column)
    begin
      self[column] = SecureRandom.urlsafe_base64
    end  ##while User.exists?(column => self[column])
  end

and use the above logic in your own method which sends email

 def send_reset_password_instructions(attributes={})
        user=User.find_by_email attributes['email']
        ##generate reset password token shown above
        user.create_password_reset_token 
        ##@resource=User.find_by_email(attributes['email'])
        #i used delay,you may use delay_for() or delay_until as needed from sidekiq
        UserMailer.reset_password_instructions(attributes['email']).deliver

      end
Milind
  • 4,535
  • 2
  • 26
  • 58
  • I re-implemented it up to confirmation mailer, but it still not confirming the user. What I do is: 1) put the method `create_confirmation_token` logic above to my `user.rb`; 2) Add `user.create_confirmation_token` to my custom `CustomDeviseMailer < Devise::Mailer` mailer method `def confirmation_instructions(user, token, opts={})` right before `super`. It does nothing, I still get token which I get from console and link is still not confirming – alv_721 Jul 06 '15 at 08:48
  • dont use super...else it will again fall back to default implementation...As you are overriding the default behaviour....you dont need to use super... @Vla – Milind Jul 06 '15 at 11:00