2

I have added devise :confirmable to my model and created a before_create to skip the confirmation.

before_create :skip_confirmation

def skip_confirmation
  self.skip_confirmation!
end

I have a mailer named store_mailer.rb along with appropriate views app/views/stores/mailer/confirmation_instroctions.html.erb to send out the confirmation email.

class StoreMailer < Devise::Mailer
  helper :application # gives access to all helpers defined within `application_helper`.
  include Devise::Controllers::UrlHelpers # Optional. eg. `confirmation_url`
  default template_path: 'store/mailer' # to make sure that your mailer uses the devise views
end

confirmation_instroctions.html.erb

<h2>Resend confirmation instructions</h2>

<%= form_for(resource, as: resource_name, url: confirmation_path(resource_name), html: { method: :post }) do |f| %>
  <%= devise_error_messages! %>

  <div class="field">
    <%= f.label :email %><br />
    <%= f.email_field :email, autofocus: true, value: (resource.pending_reconfirmation? ? resource.unconfirmed_email : resource.email) %>
  </div>

  <div class="actions">
    <%= f.submit "Resend confirmation instructions" %>
  </div>
<% end %>

<%= render "stores/shared/links" %>

I'm trying to send the confirmation email with this: StoreMailer.confirmation_instructions(@store).deliver

But it returns the following error: ArgumentError in TransactionsController#create wrong number of arguments (given 1, expected 2..3)

Any ideas what might be wrong?

Update 1

transaction_controlller.rb

def create
   nonce_from_the_client = params['payment_method_nonce']
   @result = Braintree::Customer.create(
    first_name: params['first_name'],
    last_name: params['last_name'],
    :payment_method_nonce => nonce_from_the_client
   )

   if @result.success?
     puts @result.customer.id
     puts @result.customer.payment_methods[0].token
     StoreMailer.confirmation_instructions(@store).deliver
     redirect_to showcase_index_path, notice: 'Subscribed, please check your inbox for confirmation'

   else
     redirect_back( fallback_location: (request.referer || root_path),
                 notice: "Something went wrong while processing your transaction. Please try again!")
   end
 end
Theopap
  • 715
  • 1
  • 10
  • 33

3 Answers3

2
@store.send_confirmation_instructions.deliver

This generates the confirmation token, as well as sends the mail.

mridula
  • 3,203
  • 3
  • 32
  • 55
2

From the title seems like you are trying to manually send the email later. The proposed solution in your question is just postponing the email, not exactly manually triggering it.

If you want to forbid sending the email immediately and then to send it manually you could do it like this:

class RegistrationsController
...
def create
...
resource.skip_confirmation_notification!
...
end

Some other place where you want to trigger the email, call:

User.first.send_confirmation_instructions

User.first - change with your user

Aleks
  • 4,866
  • 3
  • 38
  • 69
0

confirmation_instructions is a method defined in Devise::Mailer (the superclass of your StoreMailer class).
As you can see here and here, it accepts 2 mandatory arguments and 1 optional.

You're invoking the method passing just one argument.
You must pass the second argument (token) too, like the following:

def create
  # ...

  # Note: I'm not sure this is the token you really need.
  # It's your responsibility check if it's correct.
  token = @result.customer.payment_methods.first.token
  StoreMailer.confirmation_instructions(@store, token).deliver

  # ...
end
Lucas Costa
  • 1,109
  • 9
  • 16
  • Thanks for the reply @Lucas Costa, I have already tried this: `StoreMailer.confirmation_instructions(@store, token).deliver`... and returns error `undefined local variable or method token` – Theopap Jul 25 '17 at 13:55
  • This is because you don't have any `token` variable defined in the scope of your method. I just updated my answer with a fix to it. – Lucas Costa Jul 25 '17 at 13:58
  • Actually I'm after a devise confirmation token, any idea on how to implement this? – Theopap Jul 25 '17 at 15:35