ruby-on-railsrubyemaildevisegibbon

Adding users to mail chimp list on registration with Gibbon and Devise


I'm trying to figure out how to add a user to my mail chimp list when they register for my app. I'm using Gibbon for mail chimp and Devise for authentication.

Here are the relevant files (I think)..

initialize/gibbon.rb

Gibbon::API.api_key = MAILCHIMP_API_KEY
Gibbon::API.timeout = 15
Gibbon::API.throws_exceptions = false

user.rb

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
   before_create :add_to_list
   has_many :categories
   has_many :startups

   def add_to_list
      @list_id = "MY_LIST_ID"
      @gb = Gibbon::API.new
        @gb.lists.subscribe({
        :id => @list_id,
        :email => {:email => self.email},
        },
        :double_optin => false,
      })
   end

   end 
   def admin?
    role == "admin"
   end

end

I'm getting an error when I try to restart my rails server.

uninitialized constant Gibbon::API 

I've tried adding this to my gem file but it won't connect to github when I run bundle install.

gem 'gibbon', git: 'git://github.co/amro/gibbon.git'

So currently I have this in my gem file

gem 'gibbon', '~> 2.0.0'

Any suggestions on what I need to do to resolve this?

Also does anyone know of a good guide for implementing gibbon with devise? I'm not sure if I'm doing this properly. Will I need to add something to my registrations/new.html.erb file to facilitate adding the user to a list?


Solution

  • You're using the Gibbon 2 gem and the Gibbon 1 syntax. I'm assuming you're following a tutorial. The call has changed from Gibbon::API to Gibbon::Request

    So your model method will look more like:

    def add_to_list
      list_id = "<YOUR-LIST-ID>"
      @gb = Gibbon::Request.new
      subscribe = @gb.lists(list_id).members.create(body: {email_address: self.email, status: "subscribed", double_optin: false})
      # Do something with subscription errors here
    end
    

    Note that you need to change the method call in the initializer as well as the model.