问题
double opt-in confirmation email still goes through, any idea what's wrong? Was having problems with the gibbon gem, so opted for mailchimp gem instead.
Gemfile
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
index.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>
routes.rb
Rails.application.routes.draw do
root 'welcome#index'
post 'welcome/subscribe' => 'welcome#subscribe'
end
回答1:
From mailchimp-api document
subscribe(id, email, merge_vars = nil, email_type = 'html', double_optin = true, update_existing = false, replace_interests = true, send_welcome = false) ⇒ Hash
The double_optin
is the parameter which will be passed directly not like what you did.
So, it will be:
@mc.lists.subscribe(@list_id, email, nil, 'html', false)
回答2:
The answer above kept giving me errors, this worked for me:
@mc.lists.subscribe(@list_id, { "email" => email }, merge_vars = nil, email_type = 'html', double_optin = false)
来源:https://stackoverflow.com/questions/35117053/mailchimp-gem-double-optin-false-not-working