Closed DanielKehoe closed 9 years ago
Solved it with the following code:
mailchimp = Gibbon::Request.new(api_key: Rails.application.secrets.mailchimp_api_key)
mailchimp_id = Rails.application.secrets.mailchimp_list_id
result = mailchimp.lists(mailchimp_id).members.create(
body: {
:email_address => self.email,
:status => 'subscribed'
})
@asalom that's correct. I'm working on an update to the book.
Thank you, I'm really enjoying your book. It gave me the kickstart I needed to jump into the rails world. If it helps you in any way I improved my solution a little bit by handling the exceptions Gibbon raises when there are any problems with the request such as subscribing an email address more than once.
@asalom yes it'd be nice to see your exception handling code.
Just a note, that doesn't apply to the learn-rails application, but is relevant if the subscribe method runs as an Active Job: An exception doesn't disrupt the response to the user. You could handle the exception by sending an email to the administrator so you know there was a failure. Or you could install an exception notifier gem such as Rollbar. Otherwise, the exception just gets logged and you don't know unless you search the log files.
When I try to register an email that was previously registered with the new version of Gibbon I got a Gibbon::MailChimpError exception. I catch this on the subscribe method of the Visitor class and return one of the strings from the exception to the VisitorsController for showing as a flash message. The code is on GitHub: https://github.com/asalom/learn-rails/commit/40adf3779f2add384765e88bc03d9a4da3ca9902
Resolved with commit f4ff55b.
Gibbon 2.0.0 was released July 29, 2015.
When you run
bundle install
orbundle update
the newest Gibbon 2.0 gem will be installed and the application will error withEither change the Gemfile and specify
or update the code that uses Gibbon.
See https://github.com/amro/gibbon/issues/131