【问题标题】:Send devise confirmation email manually later稍后手动发送设计确认电子邮件
【发布时间】:2017-07-25 11:49:51
【问题描述】:

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

before_create :skip_confirmation

def skip_confirmation
  self.skip_confirmation!
end

我有一个名为 store_mailer.rb 的邮件程序以及适当的视图 app/views/stores/mailer/confirmation_introctions.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_introctions.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_controller.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

【问题讨论】:

  • TransactionsController#create 中有什么内容?
  • 这是我将StoreMailer.confirmation_instructions(@store).deliver 放入其中的另一种方法
  • 也分享该代码
  • 请检查更新1

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


【解决方案1】:
@store.send_confirmation_instructions.deliver

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

【讨论】:

    【解决方案2】:

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

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

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

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

    User.first.send_confirmation_instructions
    

    User.first - 随你的用户改变

    【讨论】:

      【解决方案3】:

      confirmation_instructions 是在Devise::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
      

      【讨论】:

      • 感谢@Lucas Costa 的回复,我已经尝试过了:StoreMailer.confirmation_instructions(@store, token).deliver... 并返回错误undefined local variable or method token
      • 这是因为您的方法范围内没有定义任何 token 变量。我刚刚更新了我的答案并对其进行了修复。
      • 实际上我正在寻找一个设计确认令牌,关于如何实现这个的任何想法?
      猜你喜欢
      • 2019-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-06
      • 2012-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多