Mailchimp gem double_optin 假的不起作用



双重选择加入确认电子邮件仍然通过,知道出了什么问题吗?长臂猿宝石有问题,所以选择了 mailchimp 宝石。

宝石文件

gem "mailchimp-api", "~> 2.0.4"

application_controller.rb

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception
  before_action :setup_mcapi
  def setup_mcapi
    @mc = Mailchimp::API.new('mailchimp api key goes here')
    @list_id = "list id goes here"
  end
end

welcome_controller.rb

class WelcomeController < ApplicationController
  def index
  end
  def subscribe
    email = params[:email][:address]
    if !email.blank?
      begin
        @mc.lists.subscribe(@list_id, {'email' => email}, 'double_optin' => false)
      end
      respond_to do |format|
        format.json{render :json => {:message => "Success!"}}
      end
    rescue Mailchimp::ListAlreadySubscribedError
      respond_to do |format|
        format.json{render :json => {:message => "#{email} is already subscribed to the list"}}
      end
    rescue Mailchimp::ListDoesNotExistError
      respond_to do |format|
        format.json{render :json => {:message => "The list could not be found."}}
      end
    rescue Mailchimp::Error => ex
      if ex.message
        respond_to do |format|
          format.json{render :json => {:message => "There is an error. Please enter valid email id."}}
        end
      else
        respond_to do |format|
          format.json{render :json => {:message => "An unknown error occurred."}}
        end
      end
    end
  else
    respond_to do |format|
      format.json{render :json => {:message => "Email Address Cannot be blank. Please enter valid email id."}}
    end
  end
end
end

索引.html.erb

<h3>Add a New Member</h3>
<p>Please enter your email address to subscribe to our newsletter.</p>
<%= form_tag('/welcome/subscribe', method: "post", id: "welcome", remote: "true") do -%>
   <%= email_field(:email, :address, {id: "email", placeholder: "email address"}) %>
   <%= submit_tag("Subscribe") %>
<% end %>
<div id="response">Response Will be displayed here</div>

路线.rb

 Rails.application.routes.draw do
  root 'welcome#index'
  post 'welcome/subscribe' => 'welcome#subscribe'
end

来自 mailchimp-api 文档

subscribe(id, email, merge_vars = nil, email_type = 'html', double_optin = true, update_existing = false, replace_interests = true, send_welcome = false) ⇒ 哈希

double_optin是将直接传递的参数,而不是像您所做的那样。

所以,它将是:

@mc.lists.subscribe(@list_id, email, nil, 'html', false)
上面的

答案一直给我错误,这对我有用:

@mc.lists.subscribe(@list_id, { "email" => email }, merge_vars = nil, email_type = 'html', double_optin = false)

最新更新