稍后手动发送设计确认电子邮件

时间:2017-07-25 11:49:51

标签: ruby-on-rails ruby ruby-on-rails-4 devise devise-confirmable

我已将devise :confirmable添加到我的模型中并创建了一个before_create以跳过确认。

before_create :skip_confirmation

def skip_confirmation
  self.skip_confirmation!
end

我有一个名为store_mailer.rb的邮件,以及适当的视图app / views / stores / mailer / confirmation_instroctions.html.erb发送确认电子邮件。

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" %>

我正在尝试发送确认电子邮件: StoreMailer.confirmation_instructions(@store).deliver

但它返回以下错误:ArgumentError in TransactionsController#create wrong number of arguments (given 1, expected 2..3)

任何想法可能出错?

更新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

3 个答案:

答案 0 :(得分:0)

confirmation_instructionsDevise::MailerStoreMailer类的超类)中定义的方法。
如您所见herehere,它接受​​2个必需参数和1个可选参数。

您正在调用只传递一个参数的方法 您必须传递第二个参数(token),如下所示:

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

答案 1 :(得分:0)

@store.send_confirmation_instructions.deliver

这会生成确认令牌,并发送邮件。

答案 2 :(得分:0)

从标题看来,您似乎想稍后手动发送电子邮件。您问题中建议的解决方案只是推迟发送电子邮件,而不是完全手动触发它。

如果您想禁止立即发送电子邮件,然后手动发送,您可以这样做:

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

您要触发电子邮件的其他地方,请致电:

User.first.send_confirmation_instructions

User.first-与您的用户一起更改

相关问题