1

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
Hieu Pham
  • 6,577
  • 2
  • 30
  • 50
saffront
  • 138
  • 1
  • 7

2 Answers2

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)
Hieu Pham
  • 6,577
  • 2
  • 30
  • 50
  • with that I'm now getting an ex.message error for any email that I try to submit. `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 ` – saffront Jan 31 '16 at 23:25
0

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)
Alex
  • 1
  • Try explaining how this answer is different from the one above. This might be better as a comment on the other answer instead. – Adam May 09 '17 at 20:16